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

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

Asked by 5 years ago

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)

0
Is this in a LocalScript? If not, it's because `LocalPlayer`'s for LocalScripts only. TheeDeathCaster 2368 — 5y
0
It Is LocalScript But Still, I Touch The Brick and It Won't Give Me The Trail Why? ItzJester2 12 — 5y
0
It should be a Script because it's in the ServerScriptService. User#18718 0 — 5y
0
learn english kid LoganboyInCO 150 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

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)

Ad

Answer this question