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

How do I get this Remote Event I made to work?

Asked by 2 years ago

I'm confused how Remote Events work. I made a remote event in replicated storage named "GearGet" and I have a local script in starter player scripts. The local scripts job is, when a part (pointPart) is touched, it should turn pointPart green, make a model transparent, and give a point to the player who touches pointPart.

I want pointPart to turn green and the model to be transparent only for the person who touched pointPart. I've been told to do this I need to use a remote event but I have no idea how that works. This is how I attempted to do it:

(Local script in starter player scripts):

local replicatedStorage = game.ReplicatedStorage
local GearGetRemote = replicatedStorage:WaitForChild("GearGet")

local pointPart = game.Workspace.FullGear.GearGiver
-- Colors
local blue = Color3.fromRGB(0, 0, 255)
local green = Color3.fromRGB(0,255,0)

-- Services needed
local Players = game:GetService("Players")
local canGet = true

GearGetRemote.OnClientEvent:Connect(function (otherPart)
local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
local player = game.Players:FindFirstChild(otherPart.Parent.Name)
local currentColor = pointPart.Color
local model = game.Workspace.FullGear.Gear
if humanoid and player and canGet and currentColor == blue then
    canGet = false
    player.leaderstats.Gears.Value = player.leaderstats.Gears.Value + 1
    print("Giving player gear")
    pointPart.Color = green
    for i, block in model:GetChildren() do
        if block:IsA("Part") or ("UnionOperation") then
            block.Transparency = 0.5
        elseif humanoid and player and canGet and currentColor == green then
            print("Player already recieved gear")
        end
    end
end
end)

Server script in Server Script Service:

local replicatedStorage = game.ReplicatedStorage
local GearGetRemote = replicatedStorage:WaitForChild("GearGet")

GearGetRemote.OnServerEvent:Connect(function(player,...)
    print(player.Name)
    print(...)
end)

GearGetRemote:FireAllClients()

I don't know how to fire the "GearGetRemote" Remote event when someone touches pointPart though. I feel like I'm either over thinking it, or doing it completely wrong. I looked up a youtube tutorial on how to use Remote Events and I don't really understand it.

0
A lot of your thing such as changing player values and the color of parts should be done in a server script if you want everyone to see the change based on what you are doing. manith513 121 — 2y
0
Use a .Touched event and name the object that touched the part hit. do if hit.parent:FindFirstChild("Humanoid") then local player = game.Players:GetPlayerFromCharacter(hit.Parent) if player then GearGetRemote:FireClient(player) fires to the player that touched the part. Use the functions and examples I've given you to mold your code into your desired outcome. I've started the engine, you drive... manith513 121 — 2y

1 answer

Log in to vote
1
Answered by
manith513 121
2 years ago

You are on the right track! you have a few mistakes however. First, I recommend adding the color changes in your server script, however if you are doing something different and need it in the local script, that is fine. In your server script, the remote is being fired, except the function above it isn't because the client isn't firing a message to the server. Now to your local script. Remote events when fired send the thing being fired as the first parameter, and any other variables as the other parameters. In your script on line 13, it is identifying otherpart as the server and not the player that you are trying to get. Simply remove otherpart in the function. To find the humanoid, use

local humanoid = game.Players.LocalPlayer:FindFirstChild("Humanoid") --change directory as needed.
local testplayer = game.Players.LocalPlayer -- no need to find the parent of Humanoid's name 
thats extra work.
local player = game.Players:FindFirstChild(testplayer) -- for your booleans

These fix your variables. FindFirstChild() returns a true or false I believe and not the object if you find it, so for your functions that require you to change thing in the Player, use the variable testplayer. At the end of your code (after line 29) add

GearGetRemote:FireServer() --  will make the print code in your server script work. 

The ... in your script I'm not sure what its supposed to be but add that parameter in this or that print() wont work. Let me know if you have any questions or if something isn't wrong. Hope this helped.

0
I realized you wanted to fire it whenever someone touches a part. I'll follow up with edits later when I'm free to help out. This should fix up your code in general though. manith513 121 — 2y
Ad

Answer this question