I have tried to make a shop that uses a proximityPromt to interact with it and buy a burger. Yet my code isnt working. Is there any solutions?
-- local script
local event = game.ReplicatedStorage.BuyBurger
local promt = script.Parent:FindFirstChild("ProximityPrompt")
local db = false
promt.Triggered:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
local Tokens = leaderstats:WaitForChild("Tokens")
local burger = game.ServerStorage:WaitForChild("Very Humane Hamburger"):Clone()
if Tokens >= 10 and db == true then
db = true
burger.Parent = player.Backpack
--Tokens.Value -= 10
event:FireServer()
wait()
db = false
end
end)
-- serverScript
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"
local Tokens = Instance.new("IntValue")
Tokens.Parent = leaderstats
Tokens.Name = "Tokens"
Tokens.Value = 10
local event = game.ReplicatedStorage.BuyBurger
event.OnServerEvent:Connect(function()
Tokens.Value -= 10
end)
end)
Well you shouldnt be creating an event everytime a player joins, take it outside of the playeradded event and then use the player argument to get the tokens value and then decrease it
-- new serverScript
local event = game.ReplicatedStorage.BuyBurger
event.OnServerEvent:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
local Tokens = leaderstats:WaitForChild("Tokens")
if Tokens.Value >= 10 then
Tokens.Value -= 10
local burger = game.ReplicatedStorage.Burger:Clone()
burger.Parent = player.Backpack
end
end)
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"
local Tokens = Instance.new("IntValue")
Tokens.Parent = leaderstats
Tokens.Name = "Tokens"
Tokens.Value = 10
end)
You made db true only when inside the if. And if I set db to false again, doesn’t that mean the if statement doesn’t work? How about setting db to true when the event is activated and setting it to false again at the end of the if statement?
promt.Triggered:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
local Tokens = leaderstats:WaitForChild("Tokens")
local burger = game.ServerStorage:WaitForChild("Very Humane Hamburger"):Clone()
db = true
if Tokens >= 10 and db == true then
burger.Parent = player.Backpack
--Tokens.Value -= 10
event:FireServer()
wait()
db = false
end
end)