I’m trying to create a button that gives you money when you click it but it doesnt work i know its possible to do it with remote events but if i want to have a lot of them i need to make so manny remote events isnt there a way i can just do this all in the script ive tried this but this doesnt work btw its a local script inside of the button. please tell me if there’s a way!
wait(5)
local moneyButton = script.Parent
local minValue = script.Parent.MinimumValue
local maxValue = script.Parent.MaximumValue
local npcDialogueFrame = script.Parent.Parent
local values = script.Parent.Parent.Parent.Parent.Values
local moneyMultiplier = values.MoneyMultiplier
moneyButton.MouseButton1Up:Connect(function()
local player = game.Players.LocalPlayer
local money = player:WaitForChild("leaderstats"):WaitForChild("Money")
local randomAmount = math.random(minValue.Value, maxValue.Value)
local randomAmountMultiplied = randomAmount * moneyMultiplier.Value
money.Value += randomAmountMultiplied
npcDialogueFrame.Visible = false
end)
An arbitrary wait is extremely unnecessary. Since the script is a child of the ScreenGui, there should be no issue of things not loading unless if you add them later; however, you should use WaitForChild in that scenario rather than an abritrary wait.
Are you using any DataStores to save players’ data? If yes, then using RemoteEvent is a good solution. Can you give me the reason that you want to avoid using RemoteEvent?
local event = game:GetService("ReplicatedStorage"):WaitForChild("Event")
local button = script.Parent
button.MouseButton1Click:Connect(function()
local amount = 100
event:FireServer(amount)
end)
On the server…
local event = game:GetService("ReplicatedStorage"):WaitForChild("Event")
event.OnServerEvent:Connect(function(plr,amount)
print(amount) -- prints 100
end)
local event = game:GetService("ReplicatedStorage"):WaitForChild("Event")
local button = script.Parent
local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local leaderstats = player:WaitForChild("leaderstats")
local money = leaderstats:WaitForChild("Money")
button.MouseButton1Click:Connect(function()
local amount = 100
event:FireServer(amount,money)
end)
Server:
local event = game:GetService("ReplicatedStorage"):WaitForChild("Event")
event.OnServerEvent:Connect(function(plr,amount,money)
print(amount) -- prints 100
money.Value += amount
end)