When player touches barrier, if he has enough money to unlock the barrier for him but not for other players

Hello, as the title says i have made a barrier and i want it to unlock if the player has enough money for it but the barrier only to unlock for the person buying it, i have leaderstats set up and working, i have made a way of actually getting money, the barrier works just fine but it opens for everyone on the server.

It is already a Local script and it is located inside StarterPlayer in StarterPlayerScripts.

Here’s the script i made:

local Barrier = game.Workspace.UnlockableZone1.Barrier


Barrier.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player.leaderstats.Coins.Value >= Barrier.Requirement.Value then
			player.leaderstats.Coins.Value -= Barrier.Requirement.Value
			Barrier.Transparency = 1
			Barrier.CanCollide = false
			Barrier.SurfaceGui.Enabled = false
			Barrier.CanTouch = false
		end
	end     
end)

I have tried putting the script inside ServerScriptService or inside StarterGui but it doesn’t work, the barrier opens for everyone not only just for the player buying it.
And i have tried changing the script in many different ways but still doesn’t work, if someone can help me and find the error i would appreciate it a lot.

Don’t use a local script, a server script can handle it on it’s own.

Then you need to have it where there is a check on the part touch, and if the player already owns it, to allow them to pass through.Then you can use a surface gui for the part to have effects, and make the part’s transparency 1 by default.

This didn’t solve my problem, the barrier is still opening for other players too, not just the one buying it.

You might make use of collision groups for this.

By default, everything is in the Default group.

Add two groups e.g. “Barriers” & “UnlockedBarrierPlayers” and set those two groups to not collide. Then only add players that have unlocked it to the “UnlockedBarrierPlayers” group.

1 Like

This helps with the collision but the visibility would still change for everyone else tho, thanks for helping with the collisions ill try finding a way to make the visibility only change for a player

You will have to use a method like creating server-side script, local one and use a remote event for communication.

Here are the details:

  • Create a remote event and place it in replicated storage.
  • Create a server script which will detect a barrier touch and check if the toucher is allowed to unlock the barrier. If they are, the script fires the remote event to this player.
  • Create a local script which will be listening to the earlier remote event. You can then make this barrier unlocked on this player’s machine based on that.
1 Like

Hello i have an issue, this worked for a while but i dont know why it stopped working today, maybe my friends wanted to see what scripts i have done and they changed something in the script cause im working on a game on Team Create but i cant find what they changed (if they changed anything) and why it doesn’t work, if you can please help me i would appreciate it a lot,

local Barrier = game.Workspace.UnlockableZone1.Barrier
local RemoteEvent = game.ReplicatedStorage.UnlockableZone1Event

Barrier.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player.leaderstats.Coins.Value >= Barrier.Requirement.Value then
             RemoteEvent:FireClient(game.StarterPlayer.StarterPlayerScripts.UnlockableZone1LocalScript)
		end 
     end
end)

this is the server side script

local Barrier = game.Workspace.UnlockableZone1.Barrier
local player = game.Players.LocalPlayer

			player.leaderstats.Coins.Value -= Barrier.Requirement.Value
			Barrier.Transparency = 1
			Barrier.CanCollide = false
			Barrier.SurfaceGui.Enabled = false
			Barrier.CanTouch = false

This is the local script
I cant find the issue why it opens for everyone now, i have created a remote event in replicated storage, i would appreciate it REALLY MUCH if you helped me but if you cant help me its okay, i understand.
Oh and btw by not working i mean the barrier opens for everyone on the server not just for the client opening it.

Edit: It doesn’t even open the barrier anymore the first time i tested it did but now it didn’t, i didn’t change anything in the script from then to now, i dont understand why this happened, i would appreciate any help really really much but if you cant help me i understand.

Sorry, but I don’t see that the local script is listening to the remote event?!

Also, you can’t do this on the server, the first argument here Must be a player object…

1 Like

Ohh i think that’s it, i think my friends changed that in the code and i didn’t notice, thanks ill make it again then test it to see if it works, sorry for bothering but i didn’t notice it

Hmm i dont understand, when i tested it yesterday it worked, it was the same script just the local script was actually listening to the remote event, i fixed that and i changed in the server script for the first argument to be a player object and now it works properly but i dont understand how it worked yesterday without the first argument being a player object.

Thanks for the help now the script works perfect.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.