local tt = true
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") ~= nil then
if tt == true then
tt = false
script.Parent.Transparency = 1
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 25
script.Sound:Play()
wait(1)
script.Parent:Remove()
end
end
end)
What you can do is you can create the coin using a script, and then make the coin giving system in a LocalScript. Let me explain with a small code:
Server script:
local coins = Instance.new("Folder")
coins.Name = "Coins"
coins.Parent = workspace
local coin = YourCoinLocation:Clone()
coin.Parent = workspace
LocalScript:
local player = game:GetService("Players").LocalPlayer
local tt = true
for _,coin in pairs(workspace:WaitForChild("Coins"):GetChildren()) do
coin.Touched:Connect(function(hit)
if tt then
tt = false
coin.Transparency = 1
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 25
script.Sound:Play()
wait(1)
script.Parent:Remove()
end
end)
end
What happens in a LocalScript is that the server doesn’t get involved in the code. This would make the coin disappear to the person who touched it, still remaining the same for another player.
local player = game:GetService("Players").LocalPlayer
local tt = true
for _,coin in pairs(workspace:WaitForChild("Coins"):GetChildren()) do
coin.Touched:Connect(function(hit)
if tt then
tt = false
coin.Transparency = 1
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 25
script.Sound:Play()
wait(1)
script.Parent:Remove()
end
end)
end
Server script :
-- Variables.
local datastore = game:GetService("DataStoreService")
local as1 = datastore:GetDataStore("CoinsSaveSystem")
local as2 = datastore:GetDataStore("ExpSaveSystem")
-- Function.
game.Players.PlayerAdded:Connect(function(plr)
local folderr = Instance.new("Folder", plr)
folderr.Name = "leaderstats"
local coins = Instance.new("IntValue", folderr)
coins.Name = "Coins"
local exp = Instance.new("IntValue", folderr)
exp.Name = "Experience"
-- Check the value saved or add the default new player amount.
coins.Value = as1:GetAsync(plr.UserId) or 2500
as1:SetAsync(plr.UserId, coins.Value)
exp.Value = as2:GetAsync(plr.UserId) or 0
as2:SetAsync(plr.UserId, exp.Value)
coins.Changed:connect(function()
as1:SetAsync(plr.UserId, coins.Value)
end)
exp.Changed:connect(function()
as2:SetAsync(plr.UserId, exp.Value)
end)
wait(1)
print("Leaderboard : Okay !")
end)
local coins = Instance.new("Folder")
coins.Name = "Coins"
coins.Parent = workspace
local coin = game.Players.LocalPlayer.leaderstats.coins:Clone()
coin.Parent = workspace
The leaderboard is okay. But when i collect a coin nothing happened.
I see. You are using the same variable “coins” in two places.
Edit: Also, by the “YourCoinLocation” I did not mean the value in leaderstats. I meant the actual coin in workspace that would be visible to players and which could be collected.
See my edit in the previous reply. Also, after seeing the screenshot, I will add to the edit: Move that coin behind you to ReplicatedStorage, in the place of “YourCoinLocation”, write:
Could you send me the actual script instead of a screenshot? My net is down and it’s not able to render the image.
Also, remember that sending code as screenshot is a bad practice.
I don’t know if this is still the case, but awhile back when I tried doing .Touched events on the client via a LocalScript, it just wouldn’t fire at all. If this is still the case, you’d have to do the touched event on the server and then use a remoteEvent to fire the change to just the client who touched it.
Why don’t you just use the code if it works, in a local script placed in StarterPlayerScripts, as Local Scripts only work if they are a descendant of the Player, and index items separately like instead of script.Parent do
part = workspace:WaitForChild("Part")
part.Touched:Connect(function(hit)) -- etc.
but then you will have to fire an event to transfer data for a player to the server, to add money etc. on the Server so it can store the value.
like below this you could do