Key Collection System Not Working

Hey y’all,

Today I was trying to make a key collection system with proximity prompt, and that if you collected all 5 keys it would open the safe. The key collection part does work, however I’m having trouble figuring out how to open the safe.

Output : image \

Workspace :

StarterGui : image

Key Script (Individual Keys) :

Local Script (Gives each new player “keysval” & checks if player has 5 keys) : image

The error has to do with the fact that the player doesn’t have “keysval”. I think this is because I’m using “LocalPlayer” to run the check, but I have no idea what to use instead. Please let me know!

2 Likes

I don’t know why you’re trying to open the safe from the client.
Also, add a :WaitForChild when referencing the value, as it might have trouble loading. Tell me if that works out.

1 Like

image

This should be inside a Server Script instead, you’re replicating it across the client side & assuming that there is already an Instance in there plus the PlayerAdded won’t run fast enough for it to connect in time so it’s erroring as nil

--ServerScript inside ServerScriptService
game.Players.PlayerAdded:Connect(function(Player)
    local keys = Instance.new("NumberValue")
    keys.Name = "keysval"
    keys.Parent = player
end)
1 Like

The issue you’re having is because you’re creating keysval on the client, make it a regular script, not a localscript.

And for checking keysval’s value, use the Changed event and create the Changed event in the PlayerAdded after making Keysval and use that to detect when its value changes and then check if it’s 5 or not

1 Like

What do you mean by that? The player is meant to open the safe when they have all 5 keys.

The changes are made publicly though right? This way it would only be visible to the client.

1 Like

I want the safe to only open for the player with 5 keys.

There’s something in the scripting community called: Exploiters

Exploiters can manipulate & change anything that’s a LocalScript and client-sided that they can see, that includes changing your keysval variable to whatever you want

Which is why we secure Value Objects like these inside ServerScriptService

2 Likes

Change event? Could you explain to me more about that lol, I’m not too experienced.

Doesn’t work, tested it out just now.

Doesn’t seem to be working for me.

Right so firstly your main issue is because it’s making the keysval on the Client, so move it as a regular script in ServerScriptService.

And your other issue is that it only checks keysval once, it needs to do it every time the value changes, and BaseValues, such as NumberValues, have an evnet for that, the Changed evnet! It’s the same as GetPropertyChangedSignal, but also returns the new value!

game.Players.PlayerAdded:Connect(function(Player)
    local keys = Instance.new("IntValues")
    keys.Name = "keysval"
    keys.Parent = player
	
	keys.Changed:Connect(function(newVal)
		if newVal < 5 then 
			return
		end
		--Your safe code here when player has 5 keys
	end)
end)

It detects if the keysval changed, and if it did, it checks if the new value is less than 5, and if it is, do nothing, otherwise, it’ll do the code you add afterwards for the safe

Or I think you may want it for each client only if it’s not a single player game, so everythign must be client sided, the keysval making script, the Prompt scripts, everything. Keep in mind you’ll need to move the script to somewhere localscripts can run, such as StarterPlayerScripts

2 Likes

Would this go into the local script or the serverscriptservice script?

ServerscriptService script if I’m not mistaken

1 Like

Alright that works except now: image

image

Weird. The OpenSafe is in the workspace, it’s spelled correctly and all.

image

That’s odd…Is the safe made on the Client or on the Server? Is the OpenSafe thing always there or does get added later on? What’s the full code in your SeverScriptService script?

1 Like

It’s in the game already.

image

Are you certain it’s located in the workspace? Can I see the explorer?

Maybe wait for it to exist using WaitForChild?

local openSafe = workspace:WaitForChild("OpenSafe")

game.Players.PlayerAdded:Connect(function(Player)
    local keys = Instance.new("IntValues")
    keys.Name = "keysval"
    keys.Parent = player
	
	keys.Changed:Connect(function(newVal)
		if newVal < 5 then 
			return
		end
		--Your safe code here when player has 5 keys
	end)
end)
1 Like

It’s in workspace : image

image (I changed the name of the part to see if it was that)

Okay are you looking from the Client view or the Server view? If you’re looking from the client, press on Test and press Viewing: Client so we can see what the server sees

I feel like the server isn’t seeing it exists but the client is

And yea, it’s going to open it for everyone, I had already mentioned a while ago if yuo want to make it open for 1 person, everything, the keysval and the prompt scripts must be client sided

1 Like