Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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

Log in to vote
0
Answered by 4 years ago

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)
0
If you have any problems tell me firestarroblox123 440 — 4y
Ad

Answer this question