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

How do I make this remote event work?

Asked by 5 years ago
Edited 5 years ago

So I created a remote event script so when I touch a brick it prints hello. I just used print to try and figure out what I'm doing wrong.

Here is my script:

-- Variables --
houseprice = 250
local event = game.ReplicatedStorage:WaitForChild("PurchaseGUIEvent")


-- Script --

script.Parent.Touched:Connect(function(hit)
    if hit:FindFirstChild("leaderstats") then
        if hit.leaderstats.Cash.Value >= houseprice then
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            event:FireClient(player)
        end
    end
end)

Here's what is in my local script:

local replicatedStorage = game:GetService("ReplicatedStorage")
local Gui = script.Parent

replicatedStorage.PurchaseGUIEvent.OnClientEvent:Connect(function()
    print("Hey there")
end)

Whenever I touch the brick it doesn't do anything. No errors.

1 answer

Log in to vote
0
Answered by
yHasteeD 1819 Moderation Voter
5 years ago
Edited 5 years ago

On script use if hit.Parent:FindFirstChild("Humanoid") then not if hit:FindFirstChild("leaderstats") and after this get player, check player and detect cash. and execute event

For get player with TouchEvent you need to use the hit.Parent:FindFirstChild("Humanoid")

Example:

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if player then
            print("Got player: " .. player.Name)
        end
    end
end)

Here is fixed script:

-- Variables --
houseprice = 250
local event = game.ReplicatedStorage:WaitForChild("PurchaseGUIEvent")


-- Script --

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then -- Check if is a character
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)-- Get player
        if player then -- If get player
            if player.leaderstats.Cash.Value >= houseprice then -- If cash >= of houseprice (changed hit to player)
                event:FireClient(player) -- Fire event.
            end
        end
    end
end)

Hope it helped :D


Solved your problems? put in title [SOLVED] or accept a answer.
0
Thanks! It worked. Now I can continue working on my housing code. :D TypicallyPacific 61 — 5y
Ad

Answer this question