Need Help With Script

So my mini project for a game I’m making is where players will escape a room. When they escape a room I want to reward them with in-game currency. The code below is the basics of what I’m going to be trying to do. But how do I make the button destroy only on the player’s screen who touched it? For example if player 1 touched the button, the button would destroy. However player 2 still sees the button. Help would be appreciated!

local buttonPressed = false

local Button = workspace.Button

Button.Touched:Connect(function(hit)
	if not buttonPressed then
	

		local buttonPressed = true


		wait(1)
		
		Button:Destroy()
		

		local buttonPressed = false
		
	end
end)

You need to destroy the button in a local script. You can either detect touches on the client or fire a remote event to tell the client when to destroy the button.

Wait so I just change the script to be a local one?

1 Like

Well, if you are going to give a reward to the player on the server, you’ll need server touch detection, too.

1 Like

You need to use a local script for this. Create a local script in starter GUI and paste the code in that. Make sure your code has nothing server based and only client based. To define player use:

local Player = game.Players.LocalPlayer

You will also need to do:

local Leaderstats = Player.Character.leaderstats

Instead of the standard Player.leaderstats.

My final suggestion would be to use RunService instead of a standard Wait. You can do this by doing:

local RunService = game:GetService("RunService")

RunService.Heartbeat:Wait(1)

I hope this helped! :slight_smile:

There was a problem with the local Leaderstats part of your code. It comes up with this error:

10:05:33.305 - leaderstats is not a valid member of Model “Workspace.killerdolophin2000”

Can you send me your leaderstats code? And what is the leaderstats folder defined as?

Do

local Leaderstats = Player.leaderstats

because leaderstats folder is in the player, not the character

Oh now I see what you are really trying to do

What I think you should do is combine server touch detection and client:

Client detects touch, and destroys the button
Server detects touch, gives player the reward, and blacklists the player so if they touch it again it won’t re-reward them

Im not that familiar with that kind of stuff sadly, Ill probably figure it out eventually…

I think you should make a table like this (server script)

local touched = {}

and every time a player touches it do something along the line of this

if table.find(touched,player) then return end
leaderstats.Cash = leaderstats.Cash + 1

and also do this

table.insert(touched,player)

so that way if the player touches button again it won’t give them the cash twice

This is what I have so far, I’m not really comfortable with player leaderstats lol.

local leaderstats = Player.leaderstats

local RunService = game:GetService("RunService")
local Button = workspace.Button
local buttonPressed = false

Button.Touched:Connect(function(hit)
	if not buttonPressed then


		buttonPressed = true
		
		
		wait(1)
		
		leaderstats.cash = leaderstats.cash + 1
		Button:Destroy()

		 buttonPressed = false

	end
end)

It gets this error though:

10:29:21.790 - Players.killerdolophin2000.PlayerGui.LocalScript:17: attempt to perform arithmetic (add) on Instance and number

“Cash” is a Intvalue. You can’t add a value to a instance. You would need to do this:

leaderstats.cash = leaderstats.cash.Value + 1

How did I not see that lol. It was so obvious.

One thing is that its coming up with this weird error:
10:55:04.019 - cash is not a valid member of IntValue “Players.killerdolophin2000.leaderstats”

But I dont see how cash is not a valid member of leaderstates even though it is…

Leaderstats is supposed to be a folder, not a IntValue. :man_facepalming:

I never took any tutorials or anything with leaderstats so I’m sorry.

I made it a folder named “leaderstats” instead of an int value but now its saying:

11:02:16.196 - cash is not a valid member of Folder “Players.killerdolophin2000.leaderstats”

Even though it is I think

Use leaderstats:WaitForChild(“cash”)

Use that instead of the folder?