You can write your topic however you want, but you need to answer these questions:
- What do you want to achieve? Keep it simple and clear!
I am trying to make a shop UI for my experience. When a player clicks on a button to purchase an item, if their leaderstats value called “Points” is greater than or equal to the price of the item, the price will be subtracted from their leaderstats value and the player will receive the item.
- What is the issue? Include screenshots / videos if possible!
I have created a LocalScript under the button for purchasing the item. The script fires a RemoteEvent when the button is clicked. In ReplicatedStorage, I have created a script that checks the player’s leaderstats value and handles the purchase when the RemoteEvent is fired. The problem is that when the server script checks the player’s points, the value is zero (which is the starting value). How would I get the player’s actual current points value?
- What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have looked for solutions on the Developer Hub, the Devforum, YouTube and other places, but still have not found a solution that has worked.
My Scripts:
Local script under the button for purchasing the item:
script.Parent.MouseButton1Click:Connect(function()
local remoteEvent = game.ReplicatedStorage.ClickedEvent
remoteEvent:FireServer()
end)
Server script in ServerScriptService:
local price = 10
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
print(player.leaderstats.Points.Value)
if player.leaderstats.Points.Value >= price then
print("player has enough points")
player.leaderstats.Points.Value = player.leaderstats.Points.Value - price
end
end)
Leaderstats script (I just used the one from this article on the DevHub):
local Players = game:GetService("Players")
local function leaderboardSetup(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Points = Instance.new("IntValue")
Points.Name = "Points"
Points.Value = 0 -- The starting value. This is the value that the server script prints in the output window when testing.
Points.Parent = leaderstats
end
Players.PlayerAdded:Connect(leaderboardSetup)
Script for changing the player’s Points (Local Script):
local textButton = script.Parent
local Players = game:GetService("Players")
local function clicked(player, textButton)
local player = Players.LocalPlayer
local leaderstats = player:FindFirstChild("leaderstats")
leaderstats.Points.Value += 1
end
textButton.MouseButton1Click:Connect(clicked)
When I test the experience, obtain 10 points, and try to purchase the item, the script prints that the value of the points is zero. However, the correct amount of points are shown on the leaderboard and a TextLabel used to show the points. (The TextLabel uses a LocalScript to display the player’s points value). I get no errors in the output window when I try testing. How could I fix this issue?