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

I'm trying to make a part where you touch it boost your speed. How do i get the player?

Asked by 5 years ago

I try to get the player, but this is all that comes up.

Workspace.speedGiver.speedGiverScript:4: attempt to index local 'playr' (a nil value)

`local Players = game:GetService("Players")
local playr = Players.LocalPlayer
local character = playr.Character
local humanoid = character.Humanoid

local function onTouch()

    humanoid.Walkspeed = 22

end

onTouch()```

Also, sorry I have no idea how to add the code the right way.

3 answers

Log in to vote
1
Answered by
Fifkee 2017 Community Moderator Moderation Voter
5 years ago
Edited 5 years ago

The Big Heckin Touched Function

okok so you're not gonna believe this but server scripts are not the same as localscripts. they cannot access the LocalPlayer, nor can they access the PlayerGui (rather, any client-sided descendants of a player.)

first, you should connect to the touched event to the script's parent, like this:

script.Parent.Touched:Connect(function(t)

end);

we place an anonymus function within the connect (which is cool fancy speak for creating the function in the event) and it calls the function within every contact

now, we use :FindFirstAncestorWhichIsA() and Players:GetPlayerFromCharacter(). we use FindFirstAncestorWhichIsA incase you have any nested parts, like so:

script.Parent.Touched:Connect(function(t)
    if (t:FindFirstAncestorWhichIsA('Model')) then

    end
end);

we can have two if statements, but we can be cool kids and have ONE! (and also use variables cuz cool kids use those too)

script.Parent.Touched:Connect(function(t)
    local Ancestor = t:FindFirstAncestorWhichIsA('Model');
    if (Ancestor and game:GetService('Players'):GetPlayerFromCharacter(Ancestor)) then
        local Humanoid = Ancestor:FindFirstChild('Humanoid', true)
        if (Humanoid) then
            Humanoid.WalkSpeed = 25;
        end
    end;
end);

and zoom bop bap BOW you got your code thing. if you wish, you can use a table-based debounce to make sure that other people can touch this at once, or you can use a bool-based debounce to make sure only one person can touch this at a time.

woot poot, enjoy stuff!

please upvote if helped thanks :)
1
After reading the other two answers from two people that just wanted a free answer to get some quick reputation from a beginners question, this was refreshing to read lmao. I gave you an upvote. cmgtotalyawesome 1418 — 5y
0
Thanks! Fifkee 2017 — 5y
0
Thanks. As you can tell I am obviously very experienced and well versed in lua coding, but what is the t for? odinbreaker -5 — 5y
0
The "t" is the first argument that touched provides. A lot of signals provide an argument for the function to use. In this case, "t" is the object that touched the script's parent. Fifkee 2017 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

You need to use the Touched event with the part you mentioned, then you need to use the GetPlayerFromCharacter() with hit.Parent to get the player.

Log in to vote
0
Answered by
sheepposu 561 Moderation Voter
5 years ago
Edited 5 years ago

script inside the part will look like this

script.Parent.Touched:Connect(function(hit)
    pcall(function()
        hit.Parent.Humanoid.Walkspeed = 22
    end)
end)

When something touches it, if it is from the player, then it will find the humanoid and increase the walkspeed. The pcall will deal with any errors given from other parts touching it

Answer this question