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

Can't get local script to communicate to the server?

Asked by 5 years ago

Right then, I've got this drive system for a train that i've been working on. It all works fine and dandy. However, it ajust's things like the chuffing sounds for it through the local script, and as a consequence only the driver can hear it. In future i'll be adding things like particle effects as well. What i'm aiming to do is to make it so everyone can hear it and I've read up about remote events and functions but after about a week of confusing myself I've reached the point where i could do with a bit of guidance.

Anyways, here's the code:

wait(.5)
vehicle = game.Workspace:FindFirstChild(script.Parent.Train.Value)
mouse = game.Players.LocalPlayer:GetMouse()
Chassis = vehicle.Chassis
reghinge=Chassis.Regulator.HingeConstraint
regvalue=Chassis.Regulator.Value
revhinge=Chassis.Reverser.HingeConstraint
revvalue=Chassis.Reverser.Value
cutoffhinge=Chassis.Cutoff.HingeConstraint
cutoffvalue=Chassis.Cutoff.Value
airbrakehinge=Chassis.AirBrake.HingeConstraint
airbrakevalue=Chassis.AirBrake.Value
brakeseat=vehicle.Body.BrakeSeat
powerseat=vehicle.Body.PowerSeat
driveseat = vehicle.Body.Seat
chuff=vehicle.Body.Chimney.Chuff

function KeyPress(key)
    if key == "e" then
        if regvalue.Value < 20 then
            reghinge.TargetAngle=reghinge.TargetAngle-5
            powerseat.Torque=powerseat.Torque+.07
            regvalue.Value=regvalue.Value+1
            chuff.Volume=chuff.Volume+0.03


        end
    end

    if key == "q" then
        if regvalue.Value > 0 then
            reghinge.TargetAngle=reghinge.TargetAngle+5
            powerseat.Torque=powerseat.Torque-.07
            regvalue.Value=regvalue.Value-1
            chuff.Volume=chuff.Volume-0.03

        end
    end

    if key == "w" then
        if revvalue.Value < 1 then
            revhinge.TargetAngle=revhinge.TargetAngle+15
            powerseat.Throttle=powerseat.Throttle+1
            revvalue.Value=revvalue.Value+1
        end
    end

    if key == "s" then
        if revvalue.Value > -1 then
            revhinge.TargetAngle=revhinge.TargetAngle-15
            powerseat.Throttle=powerseat.Throttle-1
            revvalue.Value=revvalue.Value-1
        end
    end

    if key == "r" then
        if cutoffvalue.Value < 4 then
            cutoffhinge.TargetAngle=cutoffhinge.TargetAngle+87
            powerseat.MaxSpeed=powerseat.MaxSpeed+9
            cutoffvalue.Value=cutoffvalue.Value+1
        end
    end

    if key == "f" then
        if cutoffvalue.Value > 0 then
            cutoffhinge.TargetAngle=cutoffhinge.TargetAngle-87
            powerseat.MaxSpeed=powerseat.MaxSpeed-9
            cutoffvalue.Value=cutoffvalue.Value-1
        end
    end

    if key == "]" then
        if airbrakevalue.Value < 6 then
            airbrakehinge.TargetAngle=airbrakehinge.TargetAngle+30
            brakeseat.Torque=brakeseat.Torque+.3
            airbrakevalue.Value=airbrakevalue.Value+1
        end
    end

    if key == "[" then
        if airbrakevalue.Value > 0 then
            airbrakehinge.TargetAngle=airbrakehinge.TargetAngle-30
            brakeseat.Torque=brakeseat.Torque-.3
            airbrakevalue.Value=airbrakevalue.Value-1
        end
    end

end


function LeaveSeat(child)
    if child.Name == "SeatWeld" then
        script.Parent:Destroy()

    end
end

mouse.KeyDown:connect(KeyPress)
script.Parent.Parent.Parent.ChildRemoved:connect(LeaveSeat)


Any help is very much appreciated!

0
Use remote events. CaptainD_veloper 290 — 5y
0
Yes, I have attempted but it confuses me which is why i'm asking for some input from the community ;) XxHURSTY 0 — 5y
1
Remote events work by passing arguments given by the OnServer or OnClientEvent functions in the opposing script to the script using either FireServer() or FireClient(). These arguments are passed to the function and the function defines them. DeceptiveCaster 3761 — 5y
0
Could you give me an example of how that would be laid out? XxHURSTY 0 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

I would use remote events (i only recently learned how to do this but it's quite useful), Insert a new remote event into ReplicatedStorage (so both Server and Local scripts can access it) Name the RemoteEvent what ever you want and go into your local script.

Under

if key == "WhichEverKeyYouWant" 

put

game:GetService("ReplicatedStorage").EventNameHere:FireServer()

(you may need a new event for every key pressed if you want them to do different things) and then in your regualr script (Server Script) put

game:GetService("ReplicatedStorage").EventNameHere.OnServerEvent:Connect(function()
---Code here (sound effects or whatever)
end)

you can repeat this process with multiple events as long as they all have seperate names sorry if this is more like a tutorial than "help" but i hope it works if i'm not giving you a good enough example i'd reccomend searching "Remote Events" on the Roblox API Reference

0
I'll give it a try, Cheers for the reply matey! XxHURSTY 0 — 5y
0
no problem FlabbyBoiii 81 — 5y
0
Hasn't seemed to work, i'm abit stumped at this lmao XxHURSTY 0 — 5y
0
If he is using arguments in his function that is receiving the event, then the event call also has to pass those arguments. DeceptiveCaster 3761 — 5y
0
Ok so it's been 9 months (oop) and apparently I forgot to tell you about arguments and parameters which is kind of the whole point of RemoteEvents, you've probably figured it out by now but if you still need help then I'll tell ya about it. (it's been 9 months I don't reckon you'd still need help now) FlabbyBoiii 81 — 5y
Ad

Answer this question