Longtime ROBLOX user who has just started scripting.
Working on a script that when a button is touched, it gives a player a trail. The code itself isn't in a local script, it's in a regular script and it works in test > play mode. I tried to move it to a local script, but then the script doesn't work.
I've been tinkering/researching with this for a few hours with no luck, would anyone be able to help me fix this? Thank you.
wait(1) function onTouched(hit) wait(1) if not game.Players.LocalPlayer.Character.LowerTorso:FindFirstChild("FairyMagicTrail") then local ReplicatedStorage = game:GetService("ReplicatedStorage") local trail = ReplicatedStorage.BloomMythixTrail:Clone() trail.Parent = game.Players.LocalPlayer.Character.LowerTorso local attachment0 = game.Players.LocalPlayer.Character.UpperBody.Left1.Attachment1 local attachment1 = game.Players.LocalPlayer.Character.UpperBody.Right2.Attachment1 trail.Attachment0 = attachment0 trail.Attachment1 = attachment1 end if game.Players.LocalPlayer.Character.LowerTorso:FindFirstChild("FairyMagicTrail") then end end script.Parent.Touched:connect(onTouched)
Get the player who touched by using the "hit" argument you've put
function onTouched(hit) wait(1) local Player = game.Players:GetPlayerFromCharacter(hit.Parent) if Player then if not Player.Character:FindFirstChild("BloomMythixTrail") then local ReplicatedStorage = game.ReplicatedStorage local trail = ReplicatedStorage.BloomMythixTrail:Clone() trail.Parent = Player.Character.LowerTorso local attachment0 = Player.Character.UpperBody.Left1.Attachment1 local attachment1 = Player.Character.UpperBody.Right2.Attachment1 trail.Attachment0 = attachment0 trail.Attachment1 = attachment1 end end end script.Parent.Touched:Connect(onTouched)
the reason it only worked in studio when using LocalPlayer is that when you're testing inside of studio, you can see in the properties of game.Players
, that you are always the local player inside of studio.