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

How to check if a player has touched a part on the server?

Basically, I need help to make a check for if the player touches a coin but it needs to be on the server, or else it is very easily exploitable. I can identify the player and the part but I don't know how to actually check if the player actually touched the part on the server.

The coin touched script. (LocalScript located in the StarterCharacterScripts folder)

local character = script.Parent 
local humanoid = character:WaitForChild("Humanoid")
local rs = game:GetService("ReplicatedStorage")
local remote = rs:WaitForChild("touchedEvent")

local debounce = false
local touched = humanoid.Touched:Connect(function(hit) -- hit = the coin
	if debounce then
		return
	end
	
	-- if part not in coin folder, return
	if hit.Parent ~= workspace.CoinStorage then
		return
	end
	
	remote:FireServer(hit) -- pass the coin to the server
	
	debounce = true
	wait(0.5)
	debounce = false
	
end)

The server script (Script located in ServerScriptService)

local rs = game:GetService("ReplicatedStorage")
local remote = rs:WaitForChild("touchedEvent")

remote.OnServerEvent:Connect(function(player, part) -- player is who touched the coin, and part is the coin itself
	
	local leaderstats = player:WaitForChild("leaderstats")
	local coins = leaderstats:WaitForChild("Coins")
	local amount = part:WaitForChild("Amount") -- numbervalue in the coin
	
	coins.Value = coins.Value + amount.Value
	part:Destroy()
	
end)

1 answer

✓ Accepted answer

Script in ServerScriptService:

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
    local coins = leaderstats:WaitForChild("Coins")
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		
		local debounce = false
		humanoid.Touched:Connect(function(hit) -- hit = the coin
		    if debounce then
		        return
			end
		
		    -- if part not in coin folder, return
		    if hit.Parent ~= workspace.CoinStorage then
		        return
		    end
			
			local amount = hit:WaitForChild("Amount") -- numbervalue in the coin
			
		   	coins.Value = coins.Value + amount.Value
			hit:Destroy()
			
		    debounce = true
		    wait(0.5)
		    debounce = false
		end)
	end)
end)

1 comment

0

If you have any problems tell me

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