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 6 years ago
Edited 6 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:

01-- Variables --
02houseprice = 250
03local event = game.ReplicatedStorage:WaitForChild("PurchaseGUIEvent")
04 
05 
06-- Script --
07 
08script.Parent.Touched:Connect(function(hit)
09    if hit:FindFirstChild("leaderstats") then
10        if hit.leaderstats.Cash.Value >= houseprice then
11            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
12            event:FireClient(player)
13        end
14    end
15end)

Here's what is in my local script:

1local replicatedStorage = game:GetService("ReplicatedStorage")
2local Gui = script.Parent
3 
4replicatedStorage.PurchaseGUIEvent.OnClientEvent:Connect(function()
5    print("Hey there")
6end)

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
6 years ago
Edited 6 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:

1script.Parent.Touched:Connect(function(hit)
2    if hit.Parent:FindFirstChild("Humanoid") then
3        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
4        if player then
5            print("Got player: " .. player.Name)
6        end
7    end
8end)

Here is fixed script:

01-- Variables --
02houseprice = 250
03local event = game.ReplicatedStorage:WaitForChild("PurchaseGUIEvent")
04 
05 
06-- Script --
07 
08script.Parent.Touched:Connect(function(hit)
09    if hit.Parent:FindFirstChild("Humanoid") then -- Check if is a character
10    local player = game.Players:GetPlayerFromCharacter(hit.Parent)-- Get player
11        if player then -- If get player
12            if player.leaderstats.Cash.Value >= houseprice then -- If cash >= of houseprice (changed hit to player)
13                event:FireClient(player) -- Fire event.
14            end
15        end
16    end
17end)

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 — 6y
Ad

Answer this question