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

I tried to make you sit when you touch a part but it says a error, How do you do it?

Asked by
RootEntry 111
7 years ago

I have a script inside a part named 'SlideTrigger' and when it touches the part it should make you sit but in the output, it says 'Workspace.SlideTrigger.Script:5: attempt to index global 'player' (a nil value)'

Can someone help, any help helps me.

local player = game.Players.LocalPlayer

workspace:FindFirstChild("SlideTrigger").Touched:connect(function(hit)
    print("Touched, now sitting")
    workspace[player.Name].Humanoid.Sit = true
end)

4 answers

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

LocalPlayer is only available in a LocalScript. Though, you can use the "hit" variable from the Touched event for a clue of who's the player. Example:

workspace:FindFirstChild("SlideTrigger").Touched:connect(function(hit)
    if game.Players:GetPlayerFromCharacter(hit.Parent) then --Players:GetPlayerFromCharacter() returns nil when  the model isn't a player's character.
        print(hit.Parent.Name .. " is sitting!")
        hit.Parent.Humanoid.Sit = true
    end
end)

Ad
Log in to vote
0
Answered by 7 years ago

Try using a Seat instead of a normal part ;)

0
Yeah, works better that way since those free-sit physics doesn't appear. MinePlayersPE1 33 — 7y
Log in to vote
0
Answered by 7 years ago

Insert a script into the part you want to be touched and then insert this script if you want a player to sit and fall.

script.Parent.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent.Humanoid.Sit = true
end
end)

If you want a player to sit on the seat then I recommend you insert a seat.

Log in to vote
0
Answered by
Wiscript 622 Moderation Voter
7 years ago

Easy mate, no worries.

Where you went wrong:

You're trying to access localplayer through a script, yet that's only possible with localscripts. Instead, you can get the player from character through "game.Players:GetPlayerFromCharacter()".

A character is a model containing the humanoid, arms, legs, torso, and scripts of a player's character. Initially this property is set to nil, and is set when the player's character spawns.

The hit in a function using the touched event will be inside the character, so therefor, the character itself is the parent of the hit.

The fixed script would look like this:


workspace:FindFirstChild("SlideTrigger").Touched:connect(function(hit) local player = game.Players:GetPlayerFromCharacter(hit.Parent) print("Touched, now sitting") workspace[player.Name].Humanoid.Sit = true end)

A more simpler script would be like this

workspace:FindFirstChild("SlideTrigger").Touched:connect(function(hit)
    print("Touched, now sitting")
    hit.Parent:WaitForChild("Humanoid").Sit = true
end)

Answer this question