How can I make my collectable tokens local?

Hi, so I have these tokens in my game, and currently use a script instead of local, I am not quite sure how I should make them local, if you know, please let me know.

Code:

script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr then
		if plr.leaderstats then
			local part = script.Parent
			part.Front.Transparency = 1
			part.Back.Transparency = 1
			part.SFX:Play()
			part.CanTouch = false
			plr.leaderstats.Tokens.Value += 1
			wait(math.random(15, 60))
			part.Front.Transparency = 0
			part.Back.Transparency = 0
			part.CanTouch = true
		end
	end
end) ```
1 Like

Tbh, it will probably be quite simple to make it local:

local player == game.Players.LocalPlayer
local RemoteEvent = game.ReplicatedStorage.RemoteEvent
script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr == player then
		if plr.leaderstats then
			local part = script.Parent
			part.Front.Transparency = 1
			part.Back.Transparency = 1
			part.SFX:Play()
			part.CanTouch = false
			RemoteEvent:FireServer()
			wait(math.random(15, 60))
			part.Front.Transparency = 0
			part.Back.Transparency = 0
			part.CanTouch = true
		end
	end
end)

Note that you can not change the leaderstats locally, so you have to use a remote event. Create a remote event in replicated storage, and put the below code in a server script located inside of serverscriptservice

local RemoteEvent = game.ReplicatedStorage.RemoteEvent
RemoteEvent.OnServerEvent:Connect(function(plr)
    plr.leaderstats.Tokens.Value += 1
end)
1 Like

Oh, thank you! So the bottom script will go into the token and the top will be a local script in servicescriptservice? Sorry I’m not quite sure where I put it.

I’ve realised a flaw in my code, being that local scripts don’t fire in workspace except under certain circumstances.

Do you have all of the coins in a single folder? This will make it easier to write the code for

I do have all the tokens in a single folder. The folder is called “Tokens” in Workspace.

Keep the remote event and the server script the same, but have this be the local script, located in StarterPlayerScripts:

local player == game.Players.LocalPlayer
local RemoteEvent = game.ReplicatedStorage.RemoteEvent
for _, Token in ipairs(game.Workspace.Tokens:GetChildren()) do
	Token.Touched:Connect(function(hit)
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr == player then
			if plr.leaderstats then
				local part = script.Parent
				part.Front.Transparency = 1
				part.Back.Transparency = 1
				part.SFX:Play()
				part.CanTouch = false
				RemoteEvent:FireServer()
				wait(math.random(15, 60))
				part.Front.Transparency = 0
				part.Back.Transparency = 0
				part.CanTouch = true
			end
		end
	end)
end

Alright thank you! So I put

local RemoteEvent = game.ReplicatedStorage.RemoteEvent
RemoteEvent.OnServerEvent:Connect(function(plr)
    plr.leaderstats.Tokens.Value += 1
end) ```
into servicescriptservice?

Yep! That goes in serverscriptservice, in a server script

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