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

All Chests Opening To The Same RemoteEvent?

I have chests that open when a player clicks on the lid. When the player clicks on the lid it fires a RemoteEvent that are located inside the chests.

When I duplicated the chests and tested them, they both opened when I opened / clicked on ONE so I simply changed one of the lid's RemoteEvents to LidToggle2 and tried to see if only one would open after that and no.

Why is this happening and how do I fix this? I even edited the code inside to their corresponding RemoteEvent names and still both continue to open even though their RemoteEvent names are different.

The Code: The LocalScript that detects the click.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Down:connect(function() -- if the player chicks then ...
	if script.Chest.Value and mouse.Target.Name == "Lock" and mouse.Target.Parent.Name == "Lid" then -- if they click the chests lock
		script.Chest.Value.LidToggle2:FireServer() -- fire an event that the script will use
	end
end) 

The Code Pt.2

script.ChestLocalScript.Chest.Value = script.Parent -- setting a value

game.Players.PlayerAdded:connect(function(player) -- when a player is added
	player.CharacterAdded:connect(function() -- and whenever their character is loaded then
		if script.Parent.Configurations.StaticObject.Value == false then -- if this is not supposed to be a static object then
			local newScript = script.ChestLocalScript:Clone() -- clone the local script
			newScript.Parent = player.PlayerGui -- and put it in the player's playerGui
		end
	end)
end)

local hinge = script.Parent.Lid.HingePart -- making it easier to access this part
local lidOpen = false -- so we know if the chest is open or closed
local gold = game.ServerStorage.Gold:Clone()

if script.Parent.Configurations.StaticObject.Value == false then -- checking if this chest is supposed to open
	hinge.BodyPosition.position = Vector3.new(hinge.Position.X,hinge.Position.Y,hinge.Position.Z)
	hinge.BodyGyro.cframe = hinge.CFrame
	for i, v in pairs(script.Parent.Lid:GetChildren()) do
		v.Anchored = false
	end
	script.Parent.Lid.Lock.ClickDetector.MaxActivationDistance = 0
else
	end

script.Parent.LidToggle2.OnServerEvent:connect(function() -- when an event is fired  then see if we are to open or close the lid and set a value to do so
	if lidOpen == false then
		hinge.BodyGyro.cframe = hinge.BodyGyro.cframe * CFrame.Angles(0,0,math.rad(-90))
		lidOpen = true
		wait(3)
		gold:Clone().Parent = game.Workspace
		gold.Position = script.Parent.Lid.Lock.Position
		wait()
		script.Parent.Despawn.Disabled = true
	wait(5)
		hinge.BodyGyro.cframe = hinge.BodyGyro.cframe * CFrame.Angles(0,0,math.rad(90))
		lidOpen = false
		script.Parent.Despawn.Disabled = false
		wait(1)
	
	end
end)

Rest of the code located in a RegularScript.

1 comment

1

Code please?

1 answer

✓ Accepted answer

Updated code

  1. LocalScript parented to game.StarterPlayer.StarterPlayerScripts You can delete Chest (ObjectValue)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Down:connect(function()
	if mouse.Target.Name == "Lock" and mouse.Target.Parent.Name == "Lid" then
		if mouse.Target.Parent.Parent:FindFirstChild("LidToggle2") then -- I don't know location of LidToggle2 so if its wrong please change it.
			mouse.Target.Parent.Parent.LidToggle2:FireServer()
		end
	end
end)
  1. Script in chest model You can delete ChestLocalScript Parented to this script :)
local hinge = script.Parent.Lid.HingePart
local lidOpen = false 
local gold = game.ServerStorage.Gold:Clone()

if script.Parent.Configurations.StaticObject.Value == false then
    hinge.BodyPosition.position = Vector3.new(hinge.Position.X,hinge.Position.Y,hinge.Position.Z)
    hinge.BodyGyro.cframe = hinge.CFrame
    for i, v in pairs(script.Parent.Lid:GetChildren()) do
        v.Anchored = false
    end
    script.Parent.Lid.Lock.ClickDetector.MaxActivationDistance = 0
else
    end

script.Parent.LidToggle2.OnServerEvent:connect(function() 
    if lidOpen == false then
        hinge.BodyGyro.cframe = hinge.BodyGyro.cframe * CFrame.Angles(0,0,math.rad(-90))
        lidOpen = true
        wait(3)
        gold:Clone().Parent = game.Workspace
        gold.Position = script.Parent.Lid.Lock.Position
        wait()
        script.Parent.Despawn.Disabled = true
    wait(5)
        hinge.BodyGyro.cframe = hinge.BodyGyro.cframe * CFrame.Angles(0,0,math.rad(90))
        lidOpen = false
        script.Parent.Despawn.Disabled = false
        wait(1)

    end
end)

4 comments

0

It keeps both chests from opening but only one opens and when i got to open the other, it opens the other one instead of that one. :c

0

Try naming with ID like chest1 chest2 chest3...

0

Updater Fixed code. Try now. Name chests like Lock1 Lock2 Lock3 Lock4 ...

0

Updated*

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