Ad
Log in to vote
0

I do not understand why my local script is not working? I accept answers

Asked by 12 days ago

My local script is in StarterPlayerScript and its enabled but when a player steps on Part both player1 and player2's partSelect Variables go up. I only want the player's Variable who steps on the part to go up. I don't believe there is anything wrong with my code but who knows. Any help would be greatly appreciated.

local object = workspace.Part
local debounce = false
local partSelect = 0

local function test(hit)
    if hit.Parent:FindFirstChild("Humanoid")then
        if debounce == false then
            debounce = true
            local player = hit.Parent
            if partSelect <= 3 then
                partSelect = partSelect + 1
            end
            if partSelect == 4 then
                partSelect = 1
            end
            local part = game.Workspace:FindFirstChild("Part" .. partSelect)
            player.HumanoidRootPart.CFrame = CFrame.new(part.Position)
            wait(1)
            debounce = false
        end
    end
end
object.Touched:Connect(test)

1 answer

Log in to vote
1
Answered by
imKirda 2248 Moderation Voter Community Moderator
12 days ago

Because when anyone touches the part the changes will apply, you have no filter to prevent other gamers from touching your part, you fix that by adding one, all you have to do is get the LocalPlayer and his character, then just check if hit is him.

local object = workspace.Part
local debounce = false
local partSelect = 0

local LocalGamer = game:GetService('Players').LocalPlayer

local function test(hit)
    local Character = hit.Parent

    if Character:FindFirstChild("Humanoid") and (Character == LocalGamer.Character) then
        if debounce == false then
            debounce = true
            ...

and statement does what it sounds like, if this and that is not false and not nil then continue.

0
Thank ya man! I still just don’t understand why the original code didn’t work since it works only on a player to player basis.. IckyTheSuperior 94 — 11d
Ad

Answer this question