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

Points from leaderstats not going down after an item bought?

So i have been scripting a shop and when someone buys something the points go down for a sec and then they go back as the points the user had. This is my script

From starter gui:

local player=script.Parent.Parent
local leaderboard=player:WaitForChild("leaderstats")
local points=leaderboard:WaitForChild("Points")

while wait(3)do
 points.Value=points.Value+1
end

From Workspace:

game.Players.ChildAdded:connect(function(player)
 local stats = Instance.new("Model",player)
 stats.Name="leaderstats"
 local money = Instance.new("IntValue",stats)
 money.Name="Points"
 money.Value=0 
end)

Also there is the script from an item from the shop

script.Parent.MouseButton1Click:connect(function()
 local RS = game:GetService("ReplicatedStorage")
 local item =  RS:WaitForChild("Gravity Coil")
 local price = 50
 local player = game.Players.LocalPlayer
 local stats = player:WaitForChild("leaderstats")

if stats.Points.Value >= price then 
  stats.Points.Value = stats.Points.Value - price
  local cloned = item:Clone()
  local cloned2 = item:Clone()
  cloned2.Parent = player.Backpack
  cloned.Parent = player.StarterGear
 end
end)

2 comments

0

Did you check to see if that if statement is being ran? That’s a good strategy to see what’s wrong. Put a print statement in there and check it out.

0

But yeah this is because you are trying to change values in a local script. Just send it to the server and with a FireServer event and make set the values in the server script

1 answer

✓ Accepted answer

You need to use a remote event. This is because localscript cannot change an IntValue serverwide, even if the value is located in the player. Add these 2 scripts, and take out ur local script. I am assuming the "Shop" is a Gui Shop. In a script:

	local RS = game:GetService("ReplicatedStorage")
local function createEvent(eventName)
	    local event = game.ReplicatedStorage:FindFirstChild(eventName)
    if not event then
	        event = Instance.new("RemoteEvent", game.ReplicatedStorage)
	        event.Name = eventName
	    end
	    return event
	end
	 
	local buy= createEvent("BuyGCoil")
		
	buy.OnServerEvent:Connect(function(player, price)
	local points = player:FindFirstChild("leaderstats"):FindFirstChild("Points")
	 local item =  RS:WaitForChild("Gravity Coil")
		points.Value = points.Value - price
	local clone = item:Clone()
	local SPclone = item:Clone()
	clone.Parent = player.Backpack
	SPclone.Parent = player.StarterGear
	end
	

In a localscript in Gui Shop, in a Ui Button:

	local price = 50
	function onClick()
	local player = game.Players.LocalPlayer
	 local stats = player:WaitForChild("leaderstats")
	if stats.Points.Value >= price then
	game.ReplicatedStorage:FindFirstChild("BuyGCoil"):FireServer(price)
	end
	end
script.Parent.Activated:Connect(onClick)

This generally works great, as long as everything is in the right place. If this isn't what you intended, comment on this to let me know. If this works, then mark this as the answer so others don't waste time! Thank you!

5 comments

0

Please clean this up

0

Does the first script has to be in an ui button?

0

The first script goes is a normal script not a local script. You should put it in serverscriptservice. What is happening is local scripts can’t communicate to the server, so you need to send it to a server script so that the server script can communicate with the server. That is done through remote events.

0

Listen to SethHienzman. The first script can go anywhere, really, but ServerScriptService is the best place to put it.

0

Got to do some fixes, but it actually worked, thank you!

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