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

Remote event works the first time but not the second?

Asked by 1 year ago

The idea here is i step on a part, its fires a remote event that will destroy the part and add a point to the players score. However when i test it in a server and studio, i step on the part once and it works, the part respawns and i step on it again, and nothing happens

Code local script, stored in StarterPlayerScripts: local RS = game:GetService("ReplicatedStorage")

wait(5) local money = workspace.moneyPart:FindFirstChildWhichIsA("Part")

local function onTouch() print("Working 1") game.ReplicatedStorage.RemoteEvent:FireServer() end

money.Touched:Connect(onTouch)

Code normal script, stored in ServerScriptService: game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player) print("working") local money = workspace.moneyPart:FindFirstChildWhichIsA("Part")

local function onTouch(otherPart)
    local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
    if humanoid then
        local player = game.Players:FindFirstChild(otherPart.Parent.Name)
        money:Destroy()
        if player then
            player.leaderstats.Money.Value = player.leaderstats.Money.Value + 1
        end
    end
end


money.Touched:Connect(onTouch)

end)

0
Just a note for those wondering. Yes, i'm very novice. loligaggin 2 — 1y
0
So where is the last code sample located? Tip: remember to always put your code in brackets so its easier to read bestshot123 38 — 1y
0
Last code sample? As in the normal script. ServerScriptServices. loligaggin 2 — 1y
0
My theory is that when the "money" part gets deleted the local script loses track of the part since it got deleted. You should just put a script in the part that tracks when it gets touched, that way when the part respawns the script is already parented to the part. bestshot123 38 — 1y

1 answer

Log in to vote
0
Answered by 1 year ago

From looking at this, it looks like you could make it completely server sided. One thing you shouldn't do is have a connection inside of a remote event unless you plan on disconnecting it.

The reason why it doesnt fire again is because you're not checking for when the part updates.

I would make it like this, all inside of your server script. (assuming theres only one part at a time)

local MoneyHolder = workspace:WaitForChild("moneyPart")
local Connection

function onTouch(otherPart)
    local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
    if humanoid then
        local player = game.Players:FindFirstChild(otherPart.Parent.Name)
        money:Destroy()
        if player then
            player.leaderstats.Money.Value = player.leaderstats.Money.Value + 1
        end
    end
    if Connection then
     Connection:Disconnect()
   end
end

local StartingMoney = MoneyHolder:FindFirstChildWhichIsA("Part")
if StartingMoney then
    Connection = StartingMoney.Touched:Connect(onTouch)
end
MoneyHolder.ChildAdded:Connect(function(object)
    if object:IsA"Part" then
        Connection = object.Touched:Connect(onTouch)
    end
end)
Ad

Answer this question