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)
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)
Try using a Seat instead of a normal part ;)
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.
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)