How to Exchange a Script for a Local Script

This is a script (Normal), it works very well. It’s a portal that unlocks with money. But when I put it in Local Script, it didn’t work, what can I do?

script.Parent.Touched:Connect(function(hit)
	local player = hit.Parent:FindFirstChild("Humanoid")
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr.leaderstats.Coins.Value > 0 then 
		wait()
		script.Disabled = true
		script.Parent.Transparency = 1
		script.Parent.CanCollide = false
		script.Parent.SurfaceGui.Enabled = false
		plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value - 5000

	end
end)

Why do you want it in a LocalScript if it works with a server script, and this won’t work in a LocalScript since the changes wouldn’t replicate?

I want to change it in a LocalScript because if it is in a (normal) script, then other people could also get into the other zone. What can I do?

That’s why I want only one person to enter the following area

Can’t you use collision groups here? If they have >= 5000 coins add them to a collision group that lets them collide with the door.

No I don’t want that, what I did was that when you click on it you get the Coins, I need to change the script for local script.

LocalScripts can’t run in workspace, try placing it in somewhere like starter character scripts

Why use a local script though? You should be using a script for this.

I already did it and it doesn’t work either

It’s that a script makes it possible for all the people on the server to enter the door when someone opens it. But not in a LocalScript

LocalScripts can’t work in workspace. You have to place it in StarterPlayerScripts, StarterPack or StarterGUI.

Mira does not work. When I tried it with another person the same thing happened

game.Workspace.Portales.PortalUno.Touched:Connect(function(hit)
	local player = hit.Parent:FindFirstChild("Humanoid")
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr.leaderstats.Coins.Value > 0 then 
		wait()
		script.Disabled = true
		game.Workspace.Portales.PortalUno.Transparency = 1
		game.Workspace.Portales.PortalUno.CanCollide = false
		game.Workspace.Portales.PortalUno.SurfaceGui.Enabled = false
		plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value - 5000

	end
end)

LocalScripts will only run if they are a descendant of either:

  • A Player’s Backpack, such as a child of a Tool
  • A Player’s character model
  • A Player’s PlayerGui
  • A Player’s PlayerScripts.
  • The ReplicatedFirst service

Also, because leaderstat points are involved, some of your code logic must be done on the server, otherwise someone could cheat by spoofing the Coins value on their client. So to achieve the exact behavior you want there will have to be some server-client communication:

Server script inside the door:

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if (player and player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Coins") and player.leaderstats.Coins.Value >= 5000) then
		game.ReplicatedStorage.DoorUnlock:FireClient(player, script.Parent)
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - 5000
	end
end)

There should be a RemoteEvent in ReplicatedStorage named “DoorUnlock”
And a LocalScript in StarterPlayer.StarterPlayerScripts with this code:

local event = game.ReplicatedStorage:WaitForChild("DoorUnlock")
if (event) then
	event.OnClientEvent:Connect(function(door)
		door.Transparency = 0.5
		door.CanCollide = false
	end)
end

With this set up, when a player touches the door, it checks on the server that they have enough coins, subtracts the coins, then sends a signal to the client to open the door, and it will then only appear to be opened for that player.

You can keep your current script, but use collision groups to allow players to walk through the portal as @sjr04 suggested. A LocalScript is unnecessary in this case.

Oh great, but could you help me with the script to save the doors in the Player, is that I do not save and not well as the script

You can use DataStoreService to save the player’s data.

I don’t want to bother you, but please help me write it, I don’t know how

If you would like to hire someone to write a script for you, please do it in #collaboration:recruitment.