Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Remove onTouched? Can't seem to get it right

So I am fairly new to scripting and I am trying to make this treasure chest give money Separate Script then remove itself locally onTouched. Nothing is working at all for me, I feel like I am approaching this wrong.

chest = script.Parent
local ting = 0 --debouncer

function onTouched(hit)
	
	if ting == 0 then --debounce check
		ting = 1 --activate debounce
		check = hit.Parent:FindFirstChild("Humanoid") --Find the human that touched the button
		
		if check ~= nil then --If a human is found, then
			local chest.Transparency = 1
			wait(1)
			local chest:Destroy()
			end
		end
		
	end
	
	ting = 0 --remove debounce
end

end

script.Parent.Touched:connect(onTouched)

4 comments

0

Maybe using a remote event server to client

0

The debouncer can be a bool value, it doesn't have to be a number.

0

Also, why are you adding local to chest.Transparency?

0

Why did you add local to chest.Transparency and chest:Destroy()? Remove the local

1 answer

Ok, so, .Touched events cannot run on the client. How LocalScripts work is that it can access all the client things and things specific to the player (guis, camera, etc.). Basically, LocalScripts can only be run on the client, and should be used for client things. A .Touched event is not one of those. What you need are RemoteEvents. They are used for client- server and vice versa communication. For example, they're used for admin panels. You're going to need to do a little more work for something like this, but it's still super easy.

Here's the code, I guess. Remember to make a RemoteEvent in ReplicatedStorage called "DeleteChest"

Server Script(put this in the chest. if it's a collection of parts, just tell me and I'll fix it.)

local chest = script.Parent
local debounce = false
local debounceTime = 1 -- how long is the chest cooldown.. wait a second

function onTouched(hit)
	if not debounce then -- you can use not to check if something is false, and if you want to check if it's true, just drop the not.
		debounce = true
		if hit.Parent then
			local hum = hit.Parent:FindFirstChildOfClass("Humanoid")
			if hum then
				print("someone has touched the chest!")
				local plr = game.Players:GetPlayerFromCharacter(hum.Parent)
				-- reward the player with whatever you get here
				game.ReplicatedStorage.DeleteChest:FireClient(plr, chest) -- fire the event for the client
				wait(debounceTime)
			else
				print("touched something that wasnt a person")
			end
			debounce = false
		end
	else
		warn("tried to run but debounce exisited")
	end
end

chest.Touched:Connect(function(hit) -- used it this way bc im bad at the other way xd
	print("touched")
	onTouched(hit)
end)

Local Script (put this in StarterPlayerScripts)

local plr = game.Players.LocalPlayer

game.ReplicatedStorage.DeleteChest.OnClientEvent:Connect(function(chest)
	chest.Transparency = 1
	wait(1)
	chest:Destroy()
end)

pls accept the answer if it worked

This discussion is preserved as it was. New answers, comments, and votes are closed.