Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why doesn't touching local parts save the given +1 point to the leader board?

So I have been following this question on the DevForum: https://devforum.roblox.com/t/how-would-i-destroy-local-parts/365544

I was stuck on this question for a while now and decided to search the web for it. And I came upon the above link. Please read it.

So once a player touches a Coin, the coin gives them +1 and then locally get's destroyed. But the method which Xiousa used. In that method, the Coin which was added to the leader board does not save once the player rejoins the game.

So this was what I tried.

local db = true
script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		if db == true then
			db = false
			script.Parent.Transparency = 1
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
			script.Sound:Play()
			wait(1)
			script.Parent.Transparency = 1
			wait(1)
			script.Parent.Transparency = 0
			
		end
	end	
end)

And then this is what Xiousa told to do:

part.Touched:connect(function(hit)
    if game.Players:FindFirstChild(hit.Parent.Name) and game.Players[hit.Parent.Name] == game.Players.LocalPlayer then -- Checks if there's a player that touched it and if that player is the LocalPlayer
        part:Destroy()
    end
end)

So I tried to do this in my code like this in a local script:

local db = true
local Coin = game.Workspace.Coin
Coin.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		if game.Players:FindFirstChild(hit.Parent.Name) and game.Players[hit.Parent.Name] == game.Players.LocalPlayer then -- Checks if there's a player that touched it and if that player is the LocalPlayer
			if db == true then
				db = false
				Coin.Transparency = 1
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
				script.Sound:Play()
				wait(1)
				Coin:Destroy()
				db = true
			end
		end
	end	
end)

But the above method works perfectly, instead it just does not save the +1 point to the leader board even though I am using data stores perfectly fine.

Any help would be much appreciated.

Thanks

2 comments

0

Please post your data store script as the error is most likely there since I can't find anything wrong this code.

0

Things that you change via localscript dont replicate, you need to use RemoteEvents/RemoteFunctions in order to do that

1 answer

✓ Accepted answer

try this: Add a remote event inside replicated storage.

	local db = true
	script.Parent.Touched:connect(function(hit)
	    if hit.Parent:FindFirstChild("Humanoid") ~= nil then
	        if db == true then	            db = false
	            script.Parent.Transparency = 1
	            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			game.ReplicatedStorage.RemoteEvent:FireServer(player)
	            script.Sound:Play()
	            wait(1)
	            script.Parent.Transparency = 1
	            wait(1)
            script.Parent.Transparency = 0
	 
	        end
	    end
	end)

On a seperate server script:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(X, player)
	            player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
end)

1 comment

0

Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.