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

Can anyone help me with remote events?

Asked by
WH_HAN 2
4 years ago
Edited 4 years ago

I'm really new to scripting, So this is probably gonna make people cringe.

What I've been doing: I've been trying to make a script that involves remote events.

What's the goal?: I'm trying to make it so that a brick (Or a piece of Toast, In my case) That spawns on the player when the E keybind is pressed.

What's the issue?: As you know, You need a local script to use a keybind, But I would like it so that everyone can see the brick (Or Toast).

What are my scripts so far?: Script A (LocalScript):

local object = game.Workspace.ToasT
local clonedObject = object:Clone()
local repStorage = game.GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("Remote")
local Key = 'E'
game:GetService("UserInputService").InputEnded:connect(function(input,IsTyping)
   if IsTyping then return end
   if input.KeyCode == Enum.KeyCode[Key] then
      remote:FireServer()
      wait(1)
   end
end)

Script B (Normal Script)

local object = game.Workspace.ToasT
local Position = game.StarterPlayer.StarterCharacter.Head.Toaster.Position
local ClonedObject = object:Clone()
local repStorage = game.GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("Remote")

remote.OnServerEvent:Connect(function(player)
    ClonedObject.Position = Vector3.new(Position)
end)

Please help.

0
what does it shows in output? Necro_las 412 — 4y
0
Line 7, Bad Argument? (Script B) WH_HAN 2 — 4y
0
Isnt that in script A? Necro_las 412 — 4y
1
because this: "if IsTyping then return end" looks suspicious sinse you had to return something. Put "if IsTyping then return nil end" just for sure. Necro_las 412 — 4y

1 answer

Log in to vote
0
Answered by
Necro_las 412 Moderation Voter
4 years ago
Edited 4 years ago

First, you cant call the player by "local player" when you are in server script, you have to use other methods, and when you clone something it exists only in memory, so you need to define its parent. You are just missing this:

ClonedObject.Parent = Workspace

Else, i can give you some tips in the code you wrote: you dont need to create the clone in local too (unless you want no lag feel on that action). Dont use a variable for Key sinse you will call it once or twice only. I would do it like this:

local object = game.Workspace.ToasT
local remote = game.ReplicatedStorage:WaitForChild("Remote")
game:GetService("UserInputService").InputEnded:connect(function(input,IsTyping)
    if IsTyping then return nil end
    if input.KeyCode == Enum.KeyCode.E then
        remote:FireServer("ActionName")
    end
end)

Server script:

local object = game.Workspace.ToasT
local ClonedObject = object:Clone()
local remote = game.ReplicatedStorage:WaitForChild("Remote")

remote.OnServerEvent:Connect(function(player, request)
    if request == "ActionName" then -- with this you can use this same remote to do other things later without having to create lots of remote events.
        ClonedObject.Position = Vector3.new(player.Character.Head.Toaster.Position)
    end 
end)
0
Sorry but ROBLOX fixed, you can say game.Workspace, but if you want to shortform it, name it workspace instead. Xapelize 2658 — 4y
0
I've tried your script, It doesn't seem to work, Or maybe Im just stupid. (Thanks for the Help BTW) WH_HAN 2 — 4y
0
Nvm I got it :D! Thanks so much for the help! Heres the (WIP) Game: https://www.roblox.com/games/5085713537/Toaster-Island WH_HAN 2 — 4y
0
I tried the game, looks very cool lol, but the E was not toasting, there is an error in the output saying "ToasT is not a valid member of workspace" Necro_las 412 — 4y
View all comments (5 more)
0
Also i just noticed that the code I wrote is trong too, there is one "end" out of place Necro_las 412 — 4y
0
Fixed now. Necro_las 412 — 4y
0
Hmm WH_HAN 2 — 4y
0
I don't understand why its saying ToasT is not a valid member... WH_HAN 2 — 4y
0
Probably that "local object = game.Workspace.ToasT", use the method game.Workspace:WaitForChild("ToasT"), it prevents the game to try to get the object when it is not loaded ingame yet, preventing error and script break. Necro_las 412 — 4y
Ad

Answer this question