Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is my walkspeed and death script not working?

Asked by 9 years ago

I am making a script and when you touch a brick, your walkspeed will go up. The error is: Player is not a valid member of Workspace. It also said: Disconnected event because of exception

local part = game.Workspace.Lvl1Boost

part.Touched:connect(function()
    game.Workspace.Player.Humanoid.WalkSpeed = 50
end)

This script is when you touch it you die. The error is: Player is not a valid member of Workspace. It also said: Disconnected event because of exception

local part = game.Workspace.DeathBed

part.Touched:connect(function()
    game.Workspace.Player.Humanoid.Health = 0
end)

NOTE - DeathBed and Lvl1Boost are both parts.

3 answers

Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

A convenient thing about functions is this convenient little '()' called the parameter, where you could store your arguments.

Arguments are things that the functions can use, or refer to.

Your argument(s) can be an object, number, strings, tables, &c.

Example function that utilizes a parameter and the argument(s) of it.

function Example(obj)
    obj.Transparency = 1
end

script.Parent.Touched:connect(Example)
-- Will make the thing that touched the parent of the script invisible.

-- You can add as many parameters as you'd like. You don't have to use all of them.
function MultiInvisiblize(obj, obj2, omg, afk)
    obj.Transparency = .5
    obj2.Transparency = 1
    omg.Reflectance = .5
    afk.Reflectance = 1
end

function MainFunction()
    -- Assigning random bricks
    local a = script.Parent.Parent.Part1
    local b = workspace.Part
    local c = script.Parent.Parent.Part2
    local d = workspace.Part2
    MultiInvisiblize(a, b, c, d)    -- Made all 4 of my variables as arguments in this function
end

script.Parent.Touched:connect(MainFunction)

Now, in your case, since you don't want to have this work in Solo/Test mode (unless if your name is 'Player'), then add the character as the argument of the function.

Example:

local part = game.Workspace.Lvl1Boost

part.Touched:connect(function(hit)
    local Humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")
    if Humanoid then
        Humanoid.WalkSpeed = 50
    end
end)
0
Should Humanoid be a local variable? raystriker6707 30 — 9y
0
Not necessarily, but it's a good habit to take up on. Redbullusa 1580 — 9y
Ad
Log in to vote
1
Answered by 9 years ago
local part = game.Workspace.Lvl1Boost

part.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        hit.Parent.Humanoid.Walkspeed = 50  
    end
end)

That should work for you. By saying game.Workspace.Player it cannot find the player because your name is not actually Player. You had the same problem on the next one...here is the correct way of doing it.

local part = game.Workspace.DeathBed

part.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        hit.Parent.Humanoid.Health = 0
    end
end)
0
If you want you can also add in an if statement to check if there is infact a humanoid before you edit it, otherwise it will error out. Let me add that in for you... PropheticExtinction 75 — 9y
Log in to vote
0
Answered by 9 years ago

for the Walkspeed thing, here is my anwser:

local part = game.Workspace.Lvl1Boost
part.Touched:connect(function(hit)
local humanoid = hit.Parent:FindFirstChild('Humanoid')
if(humanoid ~= nil) then
 Humanoid.WalkSpeed = 50
end)

for the kill brick, here is my answer:

local part = game.Workspace.DeathBed

part.Touched:connect(function(hit)
   local humanoid = hit.Parent:FindFirstChild('Humanoid')
if(humanoid ~= nil) then
humanoid.Health = 0
end)

I just did something like this. Just to say, its supposed to work. If it does not, tell me

Answer this question