I am trying to make a system where if you press a button something upgrades and gives you more coins when you touch it with a tool
There is no error in the output but it isn’t upgrading and giving you more coins
I have tried looking on dev forum and adding more things to the script but that isn’t working either
Here is my code:
--
script.Parent.Touched:Connect(function(touch)
game.StarterGui.Shop.Frame.Upgrade.MouseButton1Click:Connect(function()
if touch.Name == "Tool" then
local coins = game.ReplicatedStorage.StringValue
coins.Value += 10
wait(0.01)
script.Parent:Destroy()
end
end)
end)
script.Parent.Touched:Connect(function(touch)
plr.PlayerGui.Shop.Frame.Upgrade.MouseButton1Click:Connect(function()
if touch.Name == "Tool" then
local coins = game.ReplicatedStorage.StringValue
coins.Value = coins.Value + 10
wait(0.01)
script.Parent:Destroy()
end
end)
end)
You need to use PlayerGui, not StarterGui. Also, it is better to use coins.Value = coins.Value + 10.
The reason you need PlayerGui is because the local player is clicking the button, not the game.
If this works please put this post as a Solution! Thanks!
It’s located in the part that gets destroyed by the tool and it has the script where it gets destroyed by the tool in the same script where the upgrades coins script is there. Is that the problem?
The script won’t work unless player is defined, and what you are touching is correct. Also, it may not work properly on a localscript. Ideally you would want to send a request to the server and do the coin stuff from there.
Sorry. Yes, so the “send a request to the server” is very useful and is required if your game has Filtering Enabled. So you need to have a RemoteEvent inside of ReplicatedStorage, and then whenever the player wants to get their reward, you do:
local event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
event:FireServer("GiveCoins", playerWhoIsGettingReward)
Then, on your server script:
local event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
event.OnServerEvent:Connect(function(player,message,playerGettingReward)
if message == "GiveCoins" then
-- Give the playerGettingReward coins
end
end
I will just copy and paste the script and you tell me if theres something wrong with it
script.Parent.Touched:Connect(function(Touch)-- This is the default coin script
if Touch.Name == “Tool” then
local coins = game.ReplicatedStorage.StringValue
script.Parent:Destroy()
coins.Value += 1
end
game.StarterGui.Shop.Frame.UpgradeButton.MouseButton1Click:Connect(function(ClickButton)
if ClickButton and Touch.Name == “Tool” then
local event = game.ReplicatedStorage:WaitForChild(“RemoteEvent”)
local status = game.ReplicatedStorage.String
event.OnServerEvent:Connect(function(player,message,playerGettingReward)
if message == “GiveCoins” then
coins.Value += 10
print(“Success!”)
wait(0.01)
script.Parent:Destroy()
end
end)
end
end)
end)
If it’s in ReplicatedStorage then it will be the same for every user so this is kinda nonsense. Shouldn’t it be in a Player and maybe an Int or Number value?