game.Workspace.Chant = script.Parent Chant.Touched:Connect() local PlayerSpeed = 50
Why won't this work?
Nonono, you're setting two values to each other! Basically you're saying:
5=5 --or-- true = not false
When you create a variable, the first part needs to be the name. The correct way to do this would be:
Chant = scipt.Parent
'Chant' is the name of variable, and you're assigning that variable to the script's parent, which is a part called Chant.
Now the next part...
Chant.Touched:Connect()
When the script sees this it thinks, "When "Chant is touched, do nothing". The problem here is that you aren't telling it what to do and where to do it. Instead, when 'Chant' is touched, you should connect it to a function like this:
Chant.Touched:Connect(function() --this will run-- end)
Now whatever is in between the function will run when 'Chant' is touched!
When you use .Touched
, the part that touches it is passed as the argument in the parameter of the function. Basically that means you can find out what part touches 'Chant'. By doing this we can check if the part that touched 'Chant' is a humanoid, then if it is, we can change it's speed.
Chant.Touched:Connect(function(otherPart) --Added parameter, can be any name end)
Now what you put was:
local PlayerSpeed = 50
Of course that didn't do anything! You just created a variable named "PlayerSpeed" and then assigned it 50. You didnt' change anything about the player who touched it. Now that we have access to the thing that touched it though, we can change the player's walkspeed.
Let's say your arm touches the part. We don't want to change your arm's walkspeed right? So we would say:
Chant.Touched:Connect(function(otherPart) local character = otherPart.Parent local humanoid = character:FindFirstChild("Humanoid") end)
This basically says, "If a part touches 'Chant', run this function: the parent of the part that touched it is called character. Let's search the character and see if we can find a "Humanoid". Now we can check if the Humanoid has been found. If it has then we know it's a player and then we can change the player's WalkSpeed.
Chant.Touched:Connect(function(otherPart) local character = otherPart.Parent local humanoid = character:FindFirstChild("Humanoid") if humanoid then --if humanoid is found then humanoid.WalkSpeed = 50 end end)
You can see we checked if humanoid then
to see if the part that touched 'Chant' was a human.
WalkSpeed
is a property of a humanoid, that's how we're able to change it. Now if you test this out it should turn your walkspeed to 50 if you touch it.
It might be confusing at first, but make sure to practice, you'll get better that way. Here's the end product with comments.
local Chant = script.Parent --we set variable named "Chant" to the script's parent Chant.Touched:Connect(function(otherPart) --when touched by anything, name that other thing 'otherPart', run function local character = otherPart.Parent --sets variable to parent of 'otherPart' local humanoid = character:FindFirstChild("Humanoid") --Searches for human if humanoid then --if humanoid is found then humanoid.WalkSpeed = 50 --change humanoid's walkspeed to 50 end end)
It's a little messy, but I hope you got at least something out of it. :)
If I understand what I think you're trying to do, then...
First of all, when I think you're trying to create a variable by doing game.Workspace.Chant = script.Parent
, you should be doing local chant = script.Parent
instead.
Next, when you have Chant.Touched:Connect()
you are connecting the touched event to no function. You could do this two ways.
The first way is by making a function then connecting the event to the function.
function onTouch() -- touch stuff end Chant.Touched:Connect(onTouch)
The second way is by connecting the function directly (if that's how you would put it?)
Chant.Touched:Connect(function() -- stuff end)
Now you made a variable for the ideal character speed I assume, so to actually change the speed you have to detect the character when he touches the brick by doing
Chant.Touched:Connect(function(hit) if hit.Parent:FindFirstChild('Humanoid') then -- check to see if the parent of whatever touched "Chant" has a humanoid to see if it's a character end end)
Then, if it is a character and has a humanoid, you can set the character's walkspeed in the humanoid.
local Chant = script.Parent local PlayerSpeed = 50 Chant.Touched:Connect(function(hit) if hit.Parent:FindFirstChild('Humanoid') then -- check to see if the parent of whatever touched "Chant" has a humanoid to see if it's a character hit.Parent:FindFirstChild('Humanoid').WalkSpeed = PlayerSpeed end end)
So as long as the script is a child of a part, then that script will work.