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

PlayerId not returning value when i try to print it (why does the roblox api never actually help?)

Asked by 3 years ago

I am trying to make a part change color when it detects it is me in the localscript, and I need help. does anyone have a solution?

1 answer

Log in to vote
0
Answered by 3 years ago

Your issue is probably that you're trying to use a Localscript in the Workspace, which doesn't work. You should use normal scripts for most things that you want everyone to see, and Localscripts are mainly used for things like GUIs that are unique to each player.

I think this should work:

local Players = game:GetService("Players")

local brick = script.Parent
local yourUserId = 1108976841
local debounce = false

brick.Touched:Connect(function(hit)
    -- make sure the brick was not touched too recently
    if not debounce then
        -- make sure it was a player that touched the brick
        local player = Players:GetPlayerFromCharacter(hit.Parent)
        if player then
            -- now we know a player touched it, but we need to make sure it was you
            if player.UserId == yourUserId then
                debounce = true
                brick.BrickColor = BrickColor.random()
                wait(1) -- time until the brick can be changed again
                debounce = false
            end
        end
    end
end)
Ad

Answer this question