Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

If Player Touches Part, He Gets A Trail, Help?

If The Player Touches A Part, That Player Gets A Trail, I'm Trying To Do It But Nothing Is Happening I Get The Error: ServerScriptService.Trail:2: attempt to index local 'player' (a nil value) Anyone Can Help? Thanks

local player = game.Players.LocalPlayer
local char = player.Character
game.Workspace.Part.Touched:Connect(function()
        local trail = game.ServerStorage.Trail:Clone()
        trail.Parent = char.Head
        local attachment0 = Instance.new("Attachment", char.Head)
        attachment0.Name = "TrailAttachment0"
local attachment1 = Instance.new("Attachment", char.HumanoidRootPart)
        attachment1.Name = "TrailAttachment0"
trail.Attachment0 = attachment0
trail.Attachment1 = attachment1
    end)

4 comments

0

Is this in a LocalScript? If not, it's because LocalPlayer's for LocalScripts only.

0

It Is LocalScript But Still, I Touch The Brick and It Won't Give Me The Trail Why?

0

It should be a Script because it's in the ServerScriptService.

0

learn english kid

1 answer

✓ Accepted answer

The problem here is that your variables for the player and the character reference LocalPlayer. This is something that is specific to LocalScripts. That means that this is specific to the client's side..

What I suspect that you want instead is to make it so that any player this touches this part gets this trail. We can do this from the server side of things (in fact we have to because of the use of ServerStorage).

Here's how you do that. I added only one new part to the code which checks that whatever touched the Part is in fact a player (otherwise we get index errors because things like regular parts won't have Heads).

I also added an argument to the anonymous function you feed into the Touched event. This gives us the part that collided with our workspace.Part.

game.Workspace.Part.Touched:Connect(function(parttouched)
        local humanoid = parttouched.Parent:FindFirstChild("Humanoid")
        -- The above is how we check that this a human it's either the Humanoid or nil.
	if humanoid ~= nil then
		local char = humanoid.Parent
		local trail = game.ServerStorage.Trail:Clone()
        	trail.Parent  = char.Head
        	local attachment0 = Instance.new("Attachment", char.Head)
       		attachment0.Name = "TrailAttachment0"
		local attachment1 = Instance.new("Attachment", char.HumanoidRootPart)
        	attachment1.Name = "TrailAttachment0"
		trail.Attachment0 = attachment0
		trail.Attachment1 = attachment1
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.