Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Anyone can press e for a weapon if one player is in an area?

i'm making a lootbox script but when one player is in the area anyone can press e and get the item. I have this under StarterPlayerScripts in a localscript:

local uis = game:GetService("UserInputService")
local inArea = false
local keyPressed = false--debounce
local player = game.Players.LocalPlayer 
workspace.LootBox1.AREA.Touched:Connect(function(player)
 inArea = true
    print("entered")
    workspace.LootBox1.Part.BillboardGui.Enabled = true

     uis.InputBegan:Connect(function(input) 
       if input.UserInputType.Name == "Keyboard" and input.KeyCode.Name == "E" and inArea == true and keyPressed == false then
        keyPressed = true
        print("THIS WORKS IF PRINTED")
wait()
game.ReplicatedStorage.CloneTool:FireServer(player)
wait()
keyPressed = false
    end
    end)
    end)

        workspace.LootBox1.AREA.TouchEnded:Connect(function(player)
            workspace.LootBox1.Part.BillboardGui.Enabled = false
            inArea = false
            print("left")
        end)

If this is using the client (local) why can other players press e when someone else is in the area?

1 answer

✓ Accepted answer

I think you would need to add a check to see if the player that is in the zone is the local player

local uis = game:GetService("UserInputService")
local inArea = false
local keyPressed = false--debounce
local player = game.Players.LocalPlayer 
workspace.LootBox1.AREA.Touched:Connect(function(player)
	if player.Name == game.Players.LocalPlayer.Name then
 inArea = true
    print("entered")
    workspace.LootBox1.Part.BillboardGui.Enabled = true

     uis.InputBegan:Connect(function(input) 
       if input.UserInputType.Name == "Keyboard" and input.KeyCode.Name == "E" and inArea == true and keyPressed == false then
        keyPressed = true
        print("THIS WORKS IF PRINTED")
wait()
game.ReplicatedStorage.CloneTool:FireServer(player)
wait()
keyPressed = false
    end
    end)
	end
    end)

        workspace.LootBox1.AREA.TouchEnded:Connect(function(player)
			if player.Name == game.Players.LocalPlayer.Name then
            workspace.LootBox1.Part.BillboardGui.Enabled = false
            inArea = false
            print("left")
			end
        end)

I'm not 100% sure about this but it's worth a try

This discussion is preserved as it was. New answers, comments, and votes are closed.