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