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

Why is this trail giver button's script only working in Testplay Mode, and not in server modes?

Asked by 6 years ago

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)

0
Localplayer only exists in localscripts(and they must be inside the player) when running in online mode. lukeb50 631 — 6y
0
What's the workaround for that? Is there something else I can call to find the character that is stepping on the brick? Believix 0 — 6y

1 answer

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

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.

0
Thank you so much! This makes much more sense and I appreciate your help. If I had reputation on this site, I would definitely give your reply a thumbs up! Believix 0 — 6y
0
You're welcome. You could help me by clicking the "Accept Answer" button below my reply. creeperhunter76 554 — 6y
Ad

Answer this question