Theres a problem with an "upgrading" script i am making

  1. 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

  2. There is no error in the output but it isn’t upgrading and giving you more coins

  3. 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!

This isn’t working for me. It is just giving the default number of coins that aren’t upgraded.

Can you tell me where this script is located?

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?

I’m not sure.

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.

I do not get what you mean by that.

Can you explain more on the “send a request to the server” thing because I’m not really an advanced scripter?

Hello? Are you still here?

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

What do you mean by “playerWhoIsGettingReward”?

The player who is getting the 10 coins, so the plr

Theres an error in output saying “FireServer can only be called from the client”

Yes, you need to fire server from a localscript. Sorry if I didn’t make that clearer earlier.

There is no error in output now but its not working

Do you have the event receiver code in your Server Script?

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)

Why coins is named StringValue and is in ReplicatedStorage?

It’s named StringValue because it is a stringvalue and also I just put the stringvalue in ReplicatedStorage for no reason

I wasn’t thinking of any other place to put it at the time

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?

Yeah, there should be an IntValue for each player. Then you can do

local value = playerGettingReward.leaderstats.Coins
value.Value = value.Value + 10