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 -- |
02 | houseprice = 250 |
03 | local event = game.ReplicatedStorage:WaitForChild( "PurchaseGUIEvent" ) |
04 |
05 |
06 | -- Script -- |
07 |
08 | script.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 |
15 | end ) |
Here's what is in my local script:
1 | local replicatedStorage = game:GetService( "ReplicatedStorage" ) |
2 | local Gui = script.Parent |
3 |
4 | replicatedStorage.PurchaseGUIEvent.OnClientEvent:Connect( function () |
5 | print ( "Hey there" ) |
6 | end ) |
Whenever I touch the brick it doesn't do anything. No errors.
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:
1 | script.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 |
8 | end ) |
Here is fixed script:
01 | -- Variables -- |
02 | houseprice = 250 |
03 | local event = game.ReplicatedStorage:WaitForChild( "PurchaseGUIEvent" ) |
04 |
05 |
06 | -- Script -- |
07 |
08 | script.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 |
17 | end ) |
Hope it helped :D