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

why does is my shop UI I spent an hour making not working?

Basicaly I've made a shop UI right? I do everything as I allways do and it doesen't work here are all my scripts :

leaderstats


game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local cash = Instance.new("NumberValue")
	cash.Name = "Souls"
	cash.Value = 100
	cash.Parent = leaderstats
end)

BuyTools :


game.ReplicatedStorage.ToolEvents.BeansEvent.OnServerEvent:Connect(function(player)
	if player.leaderstats.Cash.Value >= 25 then
		player.leaderstats.Cash.Value = player.leaderstats.Cash.Value -25
		game.ServerStorage.Tools.Beans:Clone().Parent = player.Backpack
	end
end)

game.ReplicatedStorage.ToolEvents.DecoyEvent.OnServerEvent:Connect(function(player)
	if player.leaderstats.Cash.Value >= 75 then
		player.leaderstats.Cash.Value = player.leaderstats.Cash.Value -75
		game.ServerStorage.Tools.Decoy:Clone().Parent = player.Backpack
	end
end)

game.ReplicatedStorage.ToolEvents.RegenCoilEvent.OnServerEvent:Connect(function(player)
	if player.leaderstats.Cash.Value >= 75 then
		player.leaderstats.Cash.Value = player.leaderstats.Cash.Value -75
		game.ServerStorage.Tools.RegenCoil:Clone().Parent = player.Backpack
	end
end)

game.ReplicatedStorage.ToolEvents.SpeedCoilEvent.OnServerEvent:Connect(function(player)
	if player.leaderstats.Cash.Value >= 50 then
		player.leaderstats.Cash.Value = player.leaderstats.Cash.Value -50
		game.ServerStorage.Tools.SpeedCoil:Clone().Parent = player.Backpack
	end
end)

Fire Event in UI buttons :


script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.ToolEvents.SpeedCoilEvent:FireServer()
end)

In Rep storage I have all the tool events and same in server storage I have a folder whit all the tools dunno why it ain't working help???

1 answer

There are multiple things wrong here.

First of all you don't make a RemoteEvent to just buy a tool, You can do something like this:

script.Parent.MouseButton1Down:Connect(function()

	local player = script.Parent.Parent.Parent.Parent.Parent.Parent 

	local price = 1000 -- the price.

	local place = game.Lighting -- what the place of the tool is at, preferrably at lighting.

	local item = place.SwordOfDarkness -- what the name of it is.

	local leaderstats = player:WaitForChild("leaderstats") -- Awaits for leaderstats
 
if leaderstats.Points.Value >= price then -- Change the Money to your Currency Name
		leaderstats.Points.Value = leaderstats.Points.Value - price -- subs the points from price
		player.IsDarknessSwordOwned.Value = true -- this is just for datastores

		local cloned = item:Clone() -- cloning the item

		local cloned2 = item:Clone() -- cloning the item (x2)

		cloned.Parent = player.Backpack -- placing the cloned item in the players inv

		cloned2.Parent = player.StarterGear -- placing the cloned item in the players StarterGear

elseif leaderstats.Points.Value < price  then -- If the player is too poor

		script.Parent.Text = "Not Enough" -- Changes the text to let them know they suck at life

		script.Parent.TextColor3 = Color3.new(1, 0, 0) -- changes the color to match not having enough money

		wait(3) -- waits

		script.Parent.TextColor3 = Color3.new(255, 255, 255) -- returns back to the normal text and color
		script.Parent.Text = "Buy" -- returns back to the normal text and color (x2)
	end
end)

Second of all the leaderstats don't even save, here is what a saving leaderstats looks like:


local dss = game:GetService("DataStoreService")

local ds = dss:GetDataStore("data")


function saveData(plr)


    local cash = plr.leaderstats.Cash.Value -- whatever the name of your leaderstat is.
    pcall(function()

        ds:SetAsync(plr.UserId .. "-Cash", cash)
    end)
end



game.Players.PlayerAdded:Connect(function(plr)


    local ls = Instance.new("Folder")
    ls.Name = "leaderstats"
    ls.Parent = plr

    local cash = Instance.new("IntValue")
    cash.Name = "Cash"
    cash.Parent = ls

    local data = nil

    pcall(function()

        data = ds:GetAsync(plr.UserId .. "-Cash")
    end)

    cash.Value = data or 0
end)


game.Players.PlayerRemoving:Connect(saveData)

game:BindToClose(function()


    for i, plr in pairs(game.Players:GetPlayers()) do

        saveData(plr)
    end
end)

Hope this helped!, If it did make sure to mark my answer as a solution and thank you!

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