Roblox GUI Kill Brick - lua

I'm learning LUA and trying to make a GUI that spawns a brick above the players head which kills the player if touched. I'm not sure how to to connect the Touched event to a part that's spawned in with Instance.new? Here's what I got so far!
local button = script.Parent
local function kill()
button.BackgroundColor3 = Color3.fromRGB(255, 0, 4)
button.parent.BackgroundColor3 = Color3.fromRGB(255, 0, 4)
local player = game.Players.LocalPlayer.character
local killBrick = Instance.new("Part")
killBrick.Name = "Kill Brick"
killBrick.CFrame = player.Head.CFrame*CFrame.new(0,10,0)
killBrick.Parent = game.Workspace
killBrick.Color = Color3.fromRGB(255, 0, 0)
killBrick.Material = "Neon"
killBrick.BottomSurface = "Smooth"
killBrick.TopSurface = "Studs"
button.BackgroundColor3 = Color3.fromRGB(80, 80, 255)
button.parent.BackgroundColor3 = Color3.fromRGB(106, 146, 255)
end
button.MouseButton1Click:Connect(kill)

I fixed it by doing:
killBrick.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent.Humanoid.Health=0
end
end)

Related

Tween Service on GUI not working correctly

I tried to make a code that if a RemoteEvent fire on Server, then the same RemoteEvent fires the client. When a local script receives the RemoteEvent FireClient then the Tween starts but this isn't occurring. The Frame don't turns Visible too. The output don't return any error. How i can fix that?
Code below:
local framground = script.Parent.Frame.GroundSmash.GroundSmash
local punch = script.Parent.Frame.EtoPunch.PunchRecharge
local TweenS = game:GetService("TweenService")
local TweenInf = TweenInfo.new(5, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
local Tweenin = TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
local Tween = TweenS:Create(framground, TweenInf, {Size = UDim2.new(0,1, 0, 67)})
local TweenC = TweenS:Create(punch, Tweenin, {Size = UDim2.new(0,1, 0, 67)})
game.ReplicatedStorage.CombatSkills.GroundSmash.OnClientEvent:Connect(function()
framground.Visible = true
Tween:Play()
task.wait(5)
framground.Size = UDim2.new(0, 238, 0, 67)
framground.Visible = false
end)
game.ReplicatedStorage.CombatSkills.Combat.OnClientEvent:Connect(function()
punch.Visible = true
TweenC:Play()
task.wait(.5)
punch.Size = UDim2.new(0, 238, 0, 67)
punch.Visible = false
end)

How to trigger a function after tweening in ROBLOX

Does anybody know how to trigger a function call after tweening? I want to show some text after the tween stops.
Tweens come with a Completed event, and you can attach a callback to it.
In a LocalScript, you can have something like this :
local TweenService = game:GetService("TweenService")
-- find your GUI element to animate
local lp = game.Players.LocalPlayer
local exampleScreen = Instance.new("ScreenGui", lp.PlayerGui)
exampleScreen.Name = "Example"
local label = Instance.new("TextLabel")
label.Position = UDim2.new(0,0,0,0)
label.Size = UDim2.new(0, 10, 0, 10)
label.Parent = exampleScreen
-- choose some properties to animate
local goal = {}
goal.Position = UDim2.new(0.5, -20, 0.5, -100)
goal.Size = UDim2.new(0.5, 40, 0.5, 200)
-- create the tween
local time = 5 --seconds
local tweenInfo = TweenInfo.new(time)
local tween = TweenService:Create(label, tweenInfo, goal)
-- listen for when the tween is finished
tween.Completed:Connect( function(tweenState)
label.Text = "Tween Completed"
end)
-- play the tween
tween:Play()
You have to set the tween time in tweeninfo variable. Just make it wait() for the same time as the tween takes to play() and then trigger a function.

How do you move a part to the player's torso using an offset?

I've tried switching from Vector3 to CFrame and it still hasn't been working. The best I've gotten is the part appearing like 10 feat in the air, this is what I have so far.
local Dog = game:GetService("ReplicatedStorage"):WaitForChild("PetTesting")
local RunService = game:GetService("RunService")
local tweenService = game:GetService("TweenService")
local Petfollow = game:GetService("ReplicatedStorage"):WaitForChild("PetFollow")
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
wait(1)
local dc = Dog:Clone()
dc.Name = "Pet"
dc.Parent = char
local offsetCframe = CFrame.new(0, 0, 0)
dc.CFrame = char:WaitForChild("HumanoidRootPart").CFrame + offsetCframe
dc.Anchored = true
dc.CanCollide = false
dc.Transparency = 0
end)
end)
You can use a bit of CFrame manipulation as described in https://developer.roblox.com/en-us/api-reference/datatype/CFrame. I am not sure if this is the right way of doing this, so please reply if this is incorrect.
local offset = Vector3.new(0, 0, 10)
local newCFrame = humanoidRootPart.CFrame * offset

"The function Create is not a member of "UnionOperation" when trying to use the TweenService()

I am trying to create a Maze Runner game. When making the doors to open and close i scripted it using ROBLOX's built in tween service. I run and i get "The function Create is not a member of "UnionOperation"" I have never heard of this error and couldn't find a solution. I am trying to do this on a Union part. I have no idea what to do. I need the tween to work as expected (tween the part a few spaces).
TweenService = game:GetService("TweenService")
Door = script.Parent.Door2
Door1 = Door:WaitForChild("Door1")
Door2 = Door:WaitForChild("Door2")
local TweenInformationIn = TweenInfo.new(
6,
Enum.EasingStyle.Linear,
Enum.EasingDirection.In,
0,
false,
0
)
local Door1Open = {CFrame = CFrame.new(1226.993, 131.187, -769.185)}
local Door2Open = {CFrame = CFrame.new(1226.993, 131.187, -814.271)}
local Door1Close = {CFrame = CFrame.new(1226.993, 131.187, -749.831)}
local Door2Close = {CFrame = CFrame.new(1226.993, 131.187, -834.331)}
local Tween1Open = TweenService.Create(Door1, TweenInformationIn, Door1Open)
local Tween2Open = TweenService.Create(Door2, TweenInformationIn,Door2Open)
local TweenClose = TweenService.Create(Door1, TweenInformationIn, Door1Close)
local Tween2Close = TweenService.Create(Door2,TweenInformationIn,Door2Close)
Tween1Open:Play()
Tween2Open:Play()
Replace TweenService.Create with TweenService:Create
TweenService:Create(Door1, TweenInformationIn, Door1Open)
is equivalent to
TweenService.Create(TweenService, Door1, TweenInformationIn, Door1Open)
whereas you called
TweenService.Create(Door1, TweenInformationIn, Door1Open)
So inside TweenService.Create things went south because Door1 was where TweenService should have been.
There is actually a code sample in the Robolox manual that shows how to use TweenService.
https://developer.roblox.com/api-reference/function/TweenService/Create
local TweenService = game:GetService("TweenService")
local part = Instance.new("Part")
part.Position = Vector3.new(0, 10, 0)
part.Anchored = true
part.Parent = game.Workspace
local tweenInfo = TweenInfo.new(
2, -- Time
Enum.EasingStyle.Linear, -- EasingStyle
Enum.EasingDirection.Out, -- EasingDirection
-1, -- RepeatCount (when less than zero the tween will loop indefinitely)
true, -- Reverses (tween will reverse once reaching it's goal)
0 -- DelayTime
)
local tween = TweenService:Create(part, tweenInfo, {Position = Vector3.new(0, 30, 0)})
tween:Play()
wait(10)
tween:Cancel() -- cancel the animation after 10 seconds

how do I fix my script made in Roblox Lua?

I'm making my script and the Script Analysis tool says
Error: (54,2) Expected , got 'end'
I thought maybe you guys might help.
Here is the code
-- to be placed in StarterPlayer > StarterPlayerScripts
local Players = game:GetService("Players")
-- wait for local player PlayerGui
local LocalPlayer = Players.LocalPlayer
local playerGui = LocalPlayer:WaitForChild("PlayerGui")
-- create a ScreenGui
local screenGui = Instance.new("ScreenGui", playerGui)
-- create a holder for our bar
local frame = Instance.new("Frame", screenGui)
frame.AnchorPoint = Vector2.new(0.0, 0.0)
frame.Position = UDim2.new(0.0, 0, 0.0, 0)
frame.Size = UDim2.new(0.0, 0, 0.0, 0)
-- create a bar
local bar = Instance.new("Frame", frame)
bar.Position = UDim2.new(0, 0, 0, 0)
bar.Size = UDim2.new(1, 0, 1, 0)
bar.BackgroundColor3 = Color3.new(0, 1, 0)
-- create a sound
local sound = Instance.new("Sound", screenGui)
local lastLoudness = 0
sound.SoundId = "rbxassetid://697821987"
sound.Looped = true
sound:Play()
-- define a max loudness
local maxLoudness = 30
-- animate the amplitude bar
while true do
local amplitude = math.clamp(sound.PlaybackLoudness / maxLoudness, 0, 1)
print(80-(game.Workspace.CurrentCamera.FieldOfView))
bar.Size = UDim2.new(sound.PlaybackLoudness / maxLoudness, 0, 0, 0)
wait(0.00001)
local lastLoudness = 0
local PBS = ((sound.PlaybackLoudness/50)^(sound.PlaybackLoudness/50))
if lastLoudness~=0 then
local formula = math.abs((lastLoudness*15)) + ((lastLoudness+PBS)/1.9)
if (math.abs(PBS) > formula) == true then
game.Workspace.CurrentCamera.FieldOfView = (lastLoudness)
if game.Workspace.CurrentCamera.FieldOfView <= 10 then
print(formula, PBS)
else
game.Workspace.CurrentCamera.FieldOfView = 0
print(formula, PBS)
end
end
end
end
end
end
I genuinely Don't know how to fix the code, So...
Could you guys Help me?
I spent a long time (a couple of hours) on this and I'm legitimately stuck in the deepest pits of coding hell. And I want this fixed SO BADLY.
Also, Just To Clarify, The script is supposed to change FOV when sound.PlaybackLoudness ÷ 50 × sound.PlaybackLoudness ÷ 50 is over 0.
Egor Skriptunoff is right. Let me reformat your while-loop to show you why.
-- animate the amplitude bar
while true do
local amplitude = math.clamp(sound.PlaybackLoudness / maxLoudness, 0, 1)
print(80-(game.Workspace.CurrentCamera.FieldOfView))
bar.Size = UDim2.new(sound.PlaybackLoudness / maxLoudness, 0, 0, 0)
wait(0.00001)
local lastLoudness = 0
local PBS = ((sound.PlaybackLoudness/50)^(sound.PlaybackLoudness/50))
if lastLoudness~=0 then
local formula = math.abs((lastLoudness*15)) + ((lastLoudness+PBS)/1.9)
if (math.abs(PBS) > formula) == true then
game.Workspace.CurrentCamera.FieldOfView = (lastLoudness)
if game.Workspace.CurrentCamera.FieldOfView <= 10 then
print(formula, PBS)
else
game.Workspace.CurrentCamera.FieldOfView = 0
print(formula, PBS)
end
end
end
end
end -- <-- these bad boys don't go with anything
end -- <-- just remove them to get rid of the error
EDIT : Here's a more complete answer to help with your other script issues. You were resizing your animation stuff to 0 pixels tall, so you couldn't see anything.
-- to be placed in StarterPlayer > StarterPlayerScripts
local Players = game:GetService("Players")
-- wait for local player PlayerGui
local LocalPlayer = Players.LocalPlayer
local playerGui = LocalPlayer:WaitForChild("PlayerGui")
-- create a ScreenGui
local screenGui = Instance.new("ScreenGui", playerGui)
-- create a holder for our bar
local frame = Instance.new("Frame", screenGui)
frame.AnchorPoint = Vector2.new(0.0, 0.0)
frame.Position = UDim2.new(0.0, 0, 0.0, 0)
frame.Size = UDim2.new(1.0, 0, 0.0, 50) -- <-- this is no longer invisible
-- create a bar
local bar = Instance.new("Frame", frame)
bar.Position = UDim2.new(0, 0, 0, 0)
bar.Size = UDim2.new(1, 0, 1, 0)
bar.BackgroundColor3 = Color3.new(0, 1, 0)
-- create a sound
local sound = Instance.new("Sound", screenGui)
local lastLoudness = 0
sound.SoundId = "rbxassetid://697821987"
sound.Looped = true
sound:Play()
-- define a max loudness
local maxLoudness = 30.0 -- <-- this needed to be a decimal value
local lastLoudness = 0
-- animate the amplitude bar
while true do
-- PlaybackLoudness values range from 0 to 500, so downscale it
local sampledLoundness = (sound.PlaybackLoudness / 50)
local amplitude = math.clamp(sampledLoundness / maxLoudness, 0, 1)
print("sound values : ", sound.PlaybackLoudness, maxLoudness, amplitude)
-- animate the bar
bar.Size = UDim2.new(amplitude, 0, 1, 0) -- <-- this was squashed to 0 pixels
wait(0.00001)
-- create a camera shake effect
-- NOTE - not sure what the expected behavior is here, it just zooms in
local PBS = sampledLoundness ^ sampledLoundness
if lastLoudness ~=0 then
local formula = math.abs(lastLoudness * 15) + ((lastLoudness + PBS) / 1.9)
if (math.abs(PBS) > formula) == true then
game.Workspace.CurrentCamera.FieldOfView = lastLoudness
if game.Workspace.CurrentCamera.FieldOfView <= 10 then
print("FOV is less than 10 : ", formula, PBS)
else
game.Workspace.CurrentCamera.FieldOfView = 0
print("FOV forced to 0 : ", formula, PBS)
end
end
end
-- update lastLoudness and loop
lastLoudness = sampledLoundness
end

Resources