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

How do I make this remote event work?

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

✓ Accepted answer

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.

1 comment

0

Thanks! It worked. Now I can continue working on my housing code. :D

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